forked from btorpey/clocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockBench.java
executable file
·131 lines (111 loc) · 3.68 KB
/
ClockBench.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class ClockBench
{
static double CPU_FREQ = 1;
static final int ITERS=1000; // how many iterations to run
static final int BUCKETS=256; // how many samples to collect per iteration
public static class deltaT {
deltaT(long[] v)
{
sum=0; sum2=0; min=-1; max=0; avg=0; median=0; stdev=0;
// create vector with deltas between adjacent entries
x = v;
for (int i = 0; i < x.length-1; ++i) {
x[i] = x[i+1] - x[i];
}
count = x.length -1;
for (int i = 0; i < count; ++i) {
sum += x[i];
sum2 += (x[i] * x[i]);
if (x[i] > max) max = x[i];
if ((min == -1) || (x[i] < min)) min = x[i];
}
avg = sum / count;
median = min + ((max - min) / 2);
stdev = Math.sqrt((count * sum2 - (sum * sum)) / (count * count));
}
void print()
{
System.out.printf("%d\t%7.2f\t%7.2f\t%7.2f\t%7.2f\t%7.2f\n", count, min, max, avg, median, stdev);
}
void dump()
{
System.out.printf("%7.2f\t%7.2f\n", sum, sum2);
System.out.printf("\n");
for (int i = 0 ; i < count; ++i )
System.out.printf("[%d]=%d\t", i, x[i]);
}
long[] x;
long count;
double sum;
double sum2;
double min;
double max;
double avg;
double median;
double stdev;
};
public static long[] timestamp = new long[BUCKETS];
public static void main(String[] args)
{
if (args.length > 0) {
CPU_FREQ = Double.parseDouble(args[0]);
}
System.out.printf("%25s\t%7s\t%7s\t%7s\t%7s\t%7s\t%7s\n", "Method", "samples","min","max","avg","median","stdev");
{
for (int i = 0; i < (ITERS * BUCKETS); ++i) {
timestamp[i & 0xff] = System.nanoTime();
}
System.out.printf("%25s\t", "System.nanoTime");
deltaT x = new deltaT(timestamp);
x.print();
//x.dump();
}
{
for (int i = 0; i < (ITERS * BUCKETS); ++i) {
timestamp[i & 0xff] = SysTime.clocktime();
}
System.out.printf("%25s\t", "CLOCK_REALTIME");
deltaT x = new deltaT(timestamp);
x.print();
//x.dump();
}
{
for (int i = 0; i < (ITERS * BUCKETS); ++i) {
timestamp[i & 0xff] = SysTime.cpuidrdtsc();
}
for (int i = 0; i < BUCKETS; ++i) {
timestamp[i] = (long) ((double) timestamp[i] / CPU_FREQ);
}
System.out.printf("%25s\t", "cpuid+rdtsc");
deltaT x = new deltaT(timestamp);
x.print();
//x.dump();
}
if (System.getProperty("RDTSCP") != null)
{
for (int i = 0; i < (ITERS * BUCKETS); ++i) {
timestamp[i & 0xff] = SysTime.rdtscp();
}
for (int i = 0; i < BUCKETS; ++i) {
timestamp[i] = (long) ((double) timestamp[i] / CPU_FREQ);
}
System.out.printf("%25s\t", "rdtscp");
deltaT x = new deltaT(timestamp);
x.print();
//x.dump();
}
{
for (int i = 0; i < (ITERS * BUCKETS); ++i) {
timestamp[i & 0xff] = SysTime.rdtsc();
}
for (int i = 0; i < BUCKETS; ++i) {
timestamp[i] = (long) ((double) timestamp[i] / CPU_FREQ);
}
System.out.printf("%25s\t", "rdtsc");
deltaT x = new deltaT(timestamp);
x.print();
//x.dump();
}
System.out.printf("Using CPU frequency = %,f\n", CPU_FREQ);
}
}