1
+ package pi ;
2
+
3
+ import util .Timer ;
4
+
5
+ public class Driver {
6
+ private static long sequentialRuntime ;
7
+ private static final int ITERATIONS = 20000000 ;
8
+ // update the live value each UPDATE_EACH cycles
9
+ public static final int UPDATE_EACH = 10000 ;
10
+
11
+ public static void main (String [] args ) throws Exception {
12
+
13
+ // Sequential
14
+ PiApproximation sequential = new pi .sequential .PiSequential ();
15
+ test ("Sequential version" , sequential );
16
+ System .out .println ();
17
+
18
+ // Threads
19
+ PiApproximation threads = new pi .threads .PiThreads ();
20
+ test ("Threads version" , threads );
21
+ System .out .println ();
22
+
23
+ // Live
24
+ pi .live .PiLive live = new pi .live .PiLive ();
25
+ test ("Live version" , live );
26
+ System .out .println (" Live Pi: " +live .liveValue ()+"\n " );
27
+
28
+ // Live with sync
29
+ pi .live .sync .PiLiveWithSync liveWithSync = new pi .live .sync .PiLiveWithSync ();
30
+ test ("Live version with sync" , liveWithSync );
31
+ System .out .println (" Live Pi: " +liveWithSync .liveValue ()+"\n " );
32
+
33
+ // Live with atomic
34
+ pi .live .atomic .PiLiveWithAtomic liveWithAtomic = new pi .live .atomic .PiLiveWithAtomic ();
35
+ test ("Live version with atomic" , liveWithAtomic );
36
+ System .out .println (" Live Pi: " +liveWithAtomic .liveValue ()+"\n " );
37
+ }
38
+
39
+
40
+ private static void test (String version , PiApproximation piApproximation ) throws Exception {
41
+ // warm-up
42
+ warmup (piApproximation );
43
+ if (sequentialRuntime == 0 )
44
+ warmup (piApproximation );
45
+
46
+ Timer .start ();
47
+ double pi = piApproximation .computePi (ITERATIONS );
48
+ Timer .stop ();
49
+ System .out .println (version );
50
+ System .out .println ("-----------------------------" );
51
+ Timer .log ("Time: " );
52
+ if (sequentialRuntime == 0 )
53
+ sequentialRuntime = Timer .getRuntime ();
54
+ else
55
+ System .out .printf ("Speed-up: %.2f\n " ,sequentialRuntime /1.0 /Timer .getRuntime ());
56
+ System .out .println (" Real Pi: " +3.141592653589793238462643383279D );
57
+ System .out .println ("Estimated Pi: " +pi );
58
+ }
59
+
60
+ private static void warmup (PiApproximation piApproximation )
61
+ throws Exception {
62
+ piApproximation .computePi (ITERATIONS );
63
+ piApproximation .computePi (ITERATIONS );
64
+ }
65
+ }
0 commit comments