forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.c
More file actions
54 lines (47 loc) · 1.33 KB
/
calc.c
File metadata and controls
54 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
#include <stdlib.h>
#include <net_sdo_RandomTestJava.h>
double calcInC(int nValues) {
long d = 0;
int j;
for (j = 0; j < nValues; j++) {
int n = rand() % 100 + 1;
d += n;
}
double average = (double) d / nValues;
return average;
}
JNIEXPORT jdouble JNICALL Java_net_sdo_RandomTestJava_calc0
(JNIEnv *env, jclass obj, jint nValues) {
return calcInC(nValues);
}
jclass class = NULL;
jmethodID methodID;
JNIEXPORT jdouble JNICALL Java_net_sdo_RandomTestJava_calcFromC
(JNIEnv *env, jobject this, jint nTrials, jint nValues) {
if (class == NULL) {
class = (*env)->FindClass(env, "net/sdo/RandomTestJava");
if (class == NULL) {
fprintf(stderr, "Can't find class");
exit(-1);
}
methodID = (*env)->GetMethodID(env, class, "calc", "(I)D");
if (methodID == NULL) {
fprintf(stderr, "Can't find method");
exit(-1);
}
}
double error = 0;
int i;
for (i = 0; i < nTrials; i++) {
double average = (*env)->CallDoubleMethod(env, this, methodID, nValues);
error += 50 - average;
}
return error;
}
JNIEXPORT jint JNICALL Java_net_sdo_RandomTestJava_getCRandom
(JNIEnv *env, jobject this) {
return rand() % 100 + 1;
}