forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomTestJava.java
More file actions
96 lines (83 loc) · 2.7 KB
/
RandomTestJava.java
File metadata and controls
96 lines (83 loc) · 2.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.util.Random;
public class RandomTestJava {
private Random r = new Random();
private static int nTrials;
private static int nValues;
public static void main(String[] args) {
nTrials = Integer.parseInt(args[0]);
nValues = Integer.parseInt(args[1]);
doit(false); // warmup
doit(true);
}
public static void doit(boolean report) {
RandomTestJava rtj = new RandomTestJava();
// Java Only
double error = 0;
long then = System.currentTimeMillis();
for (int i = 0; i < nTrials; i++) {
double average = rtj.calc(nValues);
error += 50 - average;
}
long now = System.currentTimeMillis();
if (report) {
System.out.println("Error: " + error + " calcuated in Java in " + (now - then));
}
// Java Java C
error = 0;
then = System.currentTimeMillis();
for (int i = 0; i < nTrials; i++) {
double average = rtj.calcCRandom(nValues);
error += 50 - average;
}
now = System.currentTimeMillis();
if (report) {
System.out.println("Error: " + error + " calcuated in C random only in " + (now - then));
}
// Java C C
error = 0;
then = System.currentTimeMillis();
for (int i = 0; i < nTrials; i++) {
double average = rtj.calc0(nValues);
error += 50 - average;
}
now = System.currentTimeMillis();
if (report) {
System.out.println("Error: " + error + " calcuated in C in " + (now - then));
}
// C Java Java
then = System.currentTimeMillis();
error = rtj.calcFromC(nTrials, nValues);
now = System.currentTimeMillis();
if (report) {
System.out.println("Error: " + error + " calcuated from C in " + (now - then));
}
}
private native double calc0(int nValues);
private native double calcFromC(int nTrials, int nValues);
private native int getCRandom();
public double calc(int nValues) {
long d = 0;
for (int j = 0; j < nValues; j++) {
int n = r.nextInt(100) + 1;
d += n;
}
double average = d / nValues;
return average;
}
public double calcCRandom(int nValues) {
long d = 0;
for (int j = 0; j < nValues; j++) {
int n = getCRandom();
d += n;
}
double average = d / nValues;
return average;
}
static {
System.load(System.getProperty("LIBPATH") + "/libRandomTestCLibrary.so");
}
}