forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUncontendedTest.java
More file actions
83 lines (70 loc) · 2.2 KB
/
Copy pathUncontendedTest.java
File metadata and controls
83 lines (70 loc) · 2.2 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
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicLong;
public class UncontendedTest {
private static ArrayList<Long> answers = new ArrayList<>();
private static long NLOOPS = 500000000;
private static interface Getter {
public long getAndIncrement();
}
private static class AtomicGetter implements Getter {
private AtomicLong al = new AtomicLong();
public long getAndIncrement() {
return al.getAndIncrement();
}
}
private static class SyncGetter implements Getter {
private long l;
@Override
public synchronized long getAndIncrement() {
return l++;
}
}
private static class UnsyncGetter implements Getter {
private long l;
@Override
public long getAndIncrement() {
return l++;
}
}
private Getter getter;
public UncontendedTest(Getter g) {
getter = g;
}
public void run() {
long l = 0;
for (int i = 0; i < NLOOPS; i++) {
l += getter.getAndIncrement();
}
answers.add(l);
}
public static void main(String[] args) {
// Warmup
AtomicGetter atomic = new AtomicGetter();
SyncGetter sync = new SyncGetter();
UnsyncGetter unsync = new UnsyncGetter();
doLoop(atomic);
doLoop(sync);
doLoop(unsync);
//Time
long syncTime = doLoop(sync);
System.out.println("Sync calc: " + answers);
long atomicTime = doLoop(atomic);
System.out.println("Atomic calc: " + answers);
long unsyncTime = doLoop(unsync);
System.out.println("Unsync calc: " + answers);
System.out.println("Time for atomic: " + atomicTime);
System.out.println("Time for synchronous: " + syncTime);
System.out.println("Time for unsync: " + unsyncTime);
}
private static long doLoop(Getter g) {
UncontendedTest uct = new UncontendedTest(g);
long then = System.currentTimeMillis();
uct.run();
long now = System.currentTimeMillis();
return (now - then);
}
}