-
Notifications
You must be signed in to change notification settings - Fork 1
/
Benchmark.cs
161 lines (146 loc) · 4.97 KB
/
Benchmark.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// -*- c-basic-offset: 2; indent-tabs-mode: nil-*-
// Benchmarks of .Net threading primitives, i.e. Lazy<>, Task<>, lock
// and Interlocked.CompareExchange(), based on microbenchmarking code by
// Peter Sestoft.
//
// [email protected] * 2018-01-23
// [email protected] * 2013-06-02, 2015-05-08
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
class Benchmark {
public static void Main(String[] args) {
SystemInfo();
Mark8("lazy-create", d => {
Lazy<int> l = new Lazy<int>(() => 42);
return 0;
});
Mark8("lazy-compute", d => {
Lazy<int> l = new Lazy<int>(() => 23);
return l.Value;
});
Mark8("lazy-compute-pub", d => {
Lazy<int> l = new Lazy<int>(() => 23, LazyThreadSafetyMode.PublicationOnly);
return l.Value;
});
Mark8("lazy-compute-ex&pub", d => {
Lazy<int> l = new Lazy<int>(() => 23, LazyThreadSafetyMode.ExecutionAndPublication);
return l.Value;
});
Mark8("task-create", d => {
Task<int> t = new Task<int>(() => 23);
return d;
});
IntPtr affinity = Process.GetCurrentProcess().ProcessorAffinity;
int p = 2;
while (p <= Environment.ProcessorCount) {
Process.GetCurrentProcess().ProcessorAffinity = new IntPtr((1L << p) - 1);
Mark8(String.Format("task-run-{0}", p), d => {
Task.Run(() => 23);
return d;
});
p *= 2;
}
Process.GetCurrentProcess().ProcessorAffinity = affinity;
object o = new object();
int i = 0;
Mark8("lock", d => {
lock (o) {
++i;
}
return i;
});
object m = new object();
Mark8("cas-success", d => {
Interlocked.CompareExchange(ref m, m, o);
return 666;
});
Mark8("cas-fail", d => {
Interlocked.CompareExchange(ref m, o, o);
return 666;
});
}
// ========== Infrastructure code ==========
private static void SystemInfo() {
Console.WriteLine("# OS {0}",
Environment.OSVersion.VersionString);
Console.WriteLine("# .NET vers. {0}",
Environment.Version);
Console.WriteLine("# 64-bit OS {0}",
Environment.Is64BitOperatingSystem);
Console.WriteLine("# 64-bit proc {0}",
Environment.Is64BitProcess);
Console.WriteLine("# CPU {0}; {1} \"cores\"",
Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"),
Environment.ProcessorCount);
Console.WriteLine("# Date {0:s}",
DateTime.Now);
}
public static double Mark8(String msg, String info, Func<int,double> f,
int n = 10, double minTime = 0.25) {
int count = 1, totalCount = 0;
double dummy = 0.0, runningTime = 0.0, st = 0.0, sst = 0.0;
do {
count *= 2;
st = sst = 0.0;
for (int j=0; j<n; j++) {
Timer t = new Timer();
for (int i=0; i<count; i++)
dummy += f(i);
runningTime = t.Check();
double time = runningTime * 1e9 / count;
st += time;
sst += time * time;
totalCount += count;
}
} while (runningTime < minTime && count < Int32.MaxValue/2);
double mean = st/n, sdev = Math.Sqrt((sst - mean*mean*n)/(n-1));
Console.WriteLine("{0,-25} {1}{2,15:F1} ns {3,10:F2} {4,10:D}", msg, info, mean, sdev, count);
return dummy / totalCount;
}
public static double Mark8(String msg, Func<int,double> f,
int n = 10, double minTime = 0.25) {
return Mark8(msg, "", f, n, minTime);
}
public static double Mark8Setup(String msg, String info, Func<int,double> f,
Action setup = null, int n = 10, double minTime = 0.25) {
int count = 1, totalCount = 0;
double dummy = 0.0, runningTime = 0.0, st = 0.0, sst = 0.0;
do {
count *= 2;
st = sst = 0.0;
for (int j=0; j<n; j++) {
Timer t = new Timer();
for (int i=0; i<count; i++) {
t.Pause();
if (setup != null)
setup();
t.Play();
dummy += f(i);
}
runningTime = t.Check();
double time = runningTime * 1e9 / count;
st += time;
sst += time * time;
totalCount += count;
}
} while (runningTime < minTime && count < Int32.MaxValue/2);
double mean = st/n, sdev = Math.Sqrt((sst - mean*mean*n)/(n-1));
Console.WriteLine("{0,-25} {1}{2,15:F1} ns {3,10:F2} {4,10:D}", msg, info, mean, sdev, count);
return dummy / totalCount;
}
public static double Mark8Setup(String msg, Func<int,double> f,
Action setup = null, int n = 10, double minTime = 0.25) {
return Mark8Setup(msg, "", f, setup, n, minTime);
}
}
// Crude timing utility ----------------------------------------
public class Timer {
private readonly System.Diagnostics.Stopwatch stopwatch
= new System.Diagnostics.Stopwatch();
public Timer() { Play(); }
public double Check() { return stopwatch.ElapsedMilliseconds / 1000.0; }
public void Pause() { stopwatch.Stop(); }
public void Play() { stopwatch.Start(); }
}