-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
executable file
·194 lines (177 loc) · 5.4 KB
/
Program.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Diagnostics;
using System.Collections.Generic;
interface ITest
{
string name { get; }
void Init();
float PerformTest(long iterations);
}
class TestMathSin : ITest
{
public string name { get; }
public TestMathSin(string name) { this.name = name; }
public void Init() { }
public float PerformTest(long iterations)
{
double sum = 0.0f;
double angle_per_iteration = Math.PI * 2.0 / iterations;
for (long i = 0; i < iterations; ++i)
{
double angle = i * angle_per_iteration;
sum += Math.Sin(angle); // make sure that code isn't optimized away
}
return (float)sum;
}
}
class TestTable : ITest
{
public string name { get; }
private float[] sine;
private int tableSize;
public TestTable(string name, int tableSize)
{
this.name = name;
this.tableSize = tableSize;
}
public void Init()
{
sine = new float[tableSize];
float anglePerIteration = (float)(Math.PI * 2.0 / tableSize);
for (int i = 0; i < tableSize; ++i)
{
sine[i] = (float)Math.Sin(i * anglePerIteration);
}
}
public float PerformTest(long iterations)
{
float sum = 0.0f;
float periods_per_iteration = 0.9999f / iterations;
for (long i = 0; i < iterations; ++i)
{
float periods = i * periods_per_iteration;
int idx = (int)(periods * tableSize);
sum += sine[idx];
}
return sum;
}
}
/*
class TestTable_unsafe : ITest
{
public string name { get; }
private float[] sine;
private int tableSize;
public TestTable_unsafe(string name, int tableSize)
{
this.name = name;
this.tableSize = tableSize;
}
public void Init()
{
sine = new float[tableSize];
float anglePerIteration = (float)(Math.PI * 2.0 / tableSize);
for (int i = 0; i < tableSize; ++i)
{
sine[i] = (float)Math.Sin(i * anglePerIteration);
}
}
public float PerformTest(long iterations)
{
float sum = 0.0f;
unsafe
{
float periods_per_iteration = 0.9999f / iterations;
fixed (float* ptr = sine)
{
for (long i = 0; i < iterations; ++i)
{
float periods = i * periods_per_iteration;
int idx = (int)(periods * tableSize);
//sum += ptr[idx];
sum += *(ptr +idx);
}
}
}
return sum;
}
}
*/
class TestParabolic : ITest
{
public string name { get; }
public TestParabolic(string name) { this.name = name; }
public void Init() { }
public float PerformTest(long iterations)
{
float sum = 0.0f;
float periods_per_iteration = 1.0f / iterations;
const float PI2 = (float)(Math.PI * 2.0);
const float F = (float)(-8.0 * Math.PI);
for (long i = 0; i < iterations; ++i)
{
float p = i * periods_per_iteration;
float sine = 8 * p + F * p * Math.Abs(p * PI2);
sum += sine;
}
return sum;
}
}
public class Program
{
const long ITERATIONS = 48000L * 3000L;
private Stopwatch stopWatch = new Stopwatch();
static void Main(string[] args)
{
string output = new Program().RunTests();
Console.WriteLine(output);
}
private string Run(ITest test, long iterations)
{
string output = string.Empty;
test.Init();
output += string.Format("{0}:", test.name);
stopWatch.Reset();
stopWatch.Start();
float results = test.PerformTest(iterations);
stopWatch.Stop();
long time_ms = stopWatch.ElapsedMilliseconds;
float time_s = time_ms * 0.001f;
// Output sum just make absolutely sure it's not optimized away
output += string.Format(" {0} K iter/s ({1} iterations) (sum = {2}, stopWatch time = {3} ms)\n", Math.Round((iterations / 1000) / time_s), iterations, results, time_ms);
return output;
}
private List<ITest> GetTests()
{
return new List<ITest> {
new TestMathSin("Library Sine Test"),
new TestParabolic("Polynomial Approximation Test"),
new TestTable("Array Test (2048 samples)", 2048),
//new TestTable("Array Test (64K samples)", 1 << 16),
new TestTable("Array Test (16M samples)", 1 << 24),
//new TestTable_unsafe("Array Test [unsafe] (2048 samples)", 2048),
//new TestTable_unsafe("Array Test [unsafe] (64K samples)", 1 << 16),
//new TestTable_unsafe("Array Test [unsafe] (16M samples)", 1 << 24),
};
}
public string RunTestsFast()
{
List<ITest> tests = GetTests();
string output = string.Empty;
foreach (var test in tests)
{
output += Run(test, 100);
}
return output;
}
public string RunTests()
{
List<ITest> tests = GetTests();
string output = string.Empty;
foreach (var test in tests)
{
output += Run(test, ITERATIONS);
}
return output;
}
}