-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
46 lines (40 loc) · 1.35 KB
/
main.java
File metadata and controls
46 lines (40 loc) · 1.35 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
public class main {
public static void main(String[] args) {
// Get the number of rounds.
String strRounds = System.getenv("rounds");
if (strRounds == null) {
System.out.printf("*** The 'rounds' environment variable is not set\n");
System.exit(1);
}
long rounds = -1;
try {
rounds = Long.parseLong(strRounds);
} catch (NumberFormatException e) {
System.out.printf("*** The 'rounds' environment variable must be an integer, saw: %s\n", strRounds);
System.exit(1);
}
double sum = 0.0;
double flip = -1.0;
long tStart, tStop;
double pi;
long ix;
// Prime the caches.
for (ix = 1; ix <= rounds; ix++) {
flip *= -1.0;
sum += flip / (double) (ix + ix - 1);
}
sum = 0.0;
flip = -1.0;
// Timed test.
tStart = System.nanoTime();
for (ix = 1; ix <= rounds; ix++) {
flip *= -1.0;
sum += flip / (double) (ix + ix - 1);
}
pi = sum * 4.0;
tStop = System.nanoTime();
// Report.
double tDeltaSecs = (tStop - tStart) / 1e9;
System.out.printf("Java,%d,%.3f,%.40f\n", rounds, tDeltaSecs, pi);
}
}