ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeUnitLoops.java
Revision: 1.5
Committed: Wed Dec 31 16:44:01 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +0 -1 lines
Log Message:
remove unused imports

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6 import java.util.concurrent.*;
7
8 public class TimeUnitLoops {
9
10 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
11
12 /**
13 * False value allows aggressive inlining of cvt() calls from
14 * test(). True value prevents any inlining, requiring virtual
15 * method dispatch.
16 */
17 static final boolean PREVENT_INLINING = true;
18
19 // The following all are used by inlining prevention clause:
20 static int index = 0;
21 static final int NUNITS = 100;
22 static final TimeUnit[] units = new TimeUnit[NUNITS];
23 static {
24 TimeUnit[] v = TimeUnit.values();
25 for (int i = 0; i < NUNITS; ++i)
26 units[i] = v[rng.next() % v.length];
27 }
28
29 public static void main(String[] args) throws Exception {
30 long start = System.currentTimeMillis();
31 long s = 0;
32 for (int i = 0; i < 100; ++i) {
33 s += test();
34 if (s == start) System.out.println(" ");
35 }
36 long end = System.currentTimeMillis();
37 System.out.println("Time: " + (end - start) + " ms");
38 }
39
40 static long test() {
41 long sum = 0;
42 int x = rng.next();
43 for (int i = 0; i < 1000000; ++i) {
44 long l = (long) x + (long) x;
45 sum += cvt(l, TimeUnit.SECONDS);
46 sum += TimeUnit.MILLISECONDS.toMicros(l+2);
47 sum += cvt(l+17, TimeUnit.NANOSECONDS);
48 sum += cvt(l+42, TimeUnit.MILLISECONDS);
49 x = LoopHelpers.compute4(x);
50 }
51 return sum + x;
52 }
53
54 static long cvt(long d, TimeUnit u) {
55 if (PREVENT_INLINING) {
56 u = units[index];
57 index = (index+1) % NUNITS;
58 }
59
60 return u.toNanos(d);
61 }
62 }