-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathProgram.cs
More file actions
270 lines (197 loc) · 7.23 KB
/
Copy pathProgram.cs
File metadata and controls
270 lines (197 loc) · 7.23 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Diagnostics;
using csFastFloat;
using System.Globalization;
namespace BenchmarkHandCoded
{
internal class Program
{
public delegate double ParsingFunc(string[] values);
public delegate double ParsingFuncUTF8(byte[][] values);
static internal Tuple<double, double, double> time_it_ns<T>(string[] lines, ParsingFunc sut, long repeat)
{
double average = 0;
double average_tpr = 0; // tick per run
double min_value = double.MaxValue;
// warmup
for (int i = 0; i != 250; i++)
{
sut(lines);
}
Stopwatch sw = new Stopwatch();
for (int i = 0; i != repeat; i++)
{
// ...;
sw.Restart();
sut(lines);
sw.Stop();
// NOTE : Elapsed.TotalMilliseconds (double) returns the total number of whole and fractional milliseconds elapsed since inception
// Elapsed.Milliseconds (int) returns the number of whole milliseconds in the current second => this is absolutely not what we need !
var dif = sw.Elapsed.TotalMilliseconds * 1000000;
average += dif;
average_tpr += sw.ElapsedTicks;
min_value = min_value < dif ? min_value : dif;
}
average /= repeat;
average_tpr /= repeat;
return new Tuple<double, double, double>(min_value, average,average_tpr);
}
static internal Tuple<double, double, double> time_it_ns_ut8<T>(byte[][] lines, ParsingFuncUTF8 sut, long repeat)
{
double average_tpr= 0;
double average = 0;
double min_value = double.MaxValue;
// warmup
for (int i = 0; i != 250; i++)
{
sut(lines);
}
Stopwatch sw = new Stopwatch();
for (int i = 0; i != repeat; i++)
{
// ...;
sw.Restart();
sut(lines);
sw.Stop();
// NOTE : Elapsed.TotalMilliseconds (double) returns the total number of whole and fractional milliseconds elapsed since inception
// Elapsed.Milliseconds (int) returns the number of whole milliseconds in the current second => this is absolutely not what we need !
var dif = sw.Elapsed.TotalMilliseconds * 1000000;
average_tpr += sw.ElapsedTicks;
average += dif;
min_value = min_value < dif ? min_value : dif;
}
average_tpr /= repeat;
average /= repeat;
return new Tuple<double, double, double>(min_value, average,average_tpr);
}
internal static string[] GetLinesFromFile(string fileName) =>
System.IO.File.ReadAllLines(fileName);
private static double find_max_fast_float(string[] lines)
{
double max = double.MinValue;
foreach (string l in lines)
{
double x = FastDoubleParser.ParseDouble(l);
max = max > x ? max : x;
}
return max;
}
private static double find_max_fast_float_try(string[] lines)
{
double max = double.MinValue;
foreach (string l in lines)
{
if (FastDoubleParser.TryParseDouble(l, out double x))
{
max = max > x ? max : x;
}
else
{
Console.WriteLine("bug");
}
}
return max;
}
private static double find_max_fast_float_utf8(byte[][] lines)
{
double max = double.MinValue;
foreach (var l in lines)
{
double x = FastDoubleParser.ParseDouble(l);
max = max > x ? max : x;
}
return max;
}
private static double find_max_fast_float_try_utf8(byte[][] lines)
{
double max = double.MinValue;
foreach (var l in lines)
{
if (FastDoubleParser.TryParseDouble(l, out double x))
{
max = max > x ? max : x;
}
else
{
Console.WriteLine("bug");
}
}
return max;
}
private static double find_max_double_parse(string[] lines)
{
double max = double.MinValue;
foreach (string l in lines)
{
double x = double.Parse(l, CultureInfo.InvariantCulture);
max = max > x ? max : x;
}
return max;
}
static private void pretty_print(double volume, uint number_of_floats, string name, Tuple<double, double, double> result)
{
double volumeMB = volume / (1024.0 * 1024.0);
Console.Write("| {0,-40}|{1,8:f2} (+/- {2:f1} % ) |", name, volumeMB * 1000000000 / result.Item1, (result.Item2 - result.Item1) * 100.0 / result.Item2);
Console.Write("{0,8:f2} | ", number_of_floats * 1000 / result.Item1);
Console.Write(" {0,8:f2} | ", (double)result.Item1 / number_of_floats);
Console.Write(" {0,8:f2} | \n", (double)result.Item3 / number_of_floats);
}
private static void Main(string[] args)
{
string fileName = @"data/canada.txt";
var lines = GetLinesFromFile(fileName);
var _linesUtf8 = Array.ConvertAll(lines, System.Text.Encoding.UTF8.GetBytes);
int volume = 0;
foreach (string l in lines)
{
volume += l.Length;
}
print_separator();
double volumeMB = volume / (1024.0 * 1024.0);
Console.WriteLine("canada.txt - Volume : {0:N2} MB",volumeMB);
process_test(lines, _linesUtf8, (double)volume);
lines = GetLinesFromFile(@"data/mesh.txt");
_linesUtf8 = Array.ConvertAll(lines, System.Text.Encoding.UTF8.GetBytes);
volume = 0;
foreach (string l in lines)
{
volume += l.Length;
}
volumeMB = volume / (1024.0 * 1024.0);
print_separator();
Console.WriteLine("mesh.txt - Volume : {0:N2} MB",volumeMB);
process_test(lines, _linesUtf8, (double)volume);
lines = GetLinesFromFile(@"data/synthetic.txt");
_linesUtf8 = Array.ConvertAll(lines, System.Text.Encoding.UTF8.GetBytes);
volume = 0;
foreach (string l in lines)
{
volume += l.Length;
}
volumeMB = volume / (1024.0 * 1024.0);
print_separator();
Console.WriteLine("synthetic.txt - Volume : {0:N2} MB",volumeMB);
process_test(lines, _linesUtf8, (double)volume);
}
private static void print_header()
{
print_separator();
Console.WriteLine("| Test name".PadRight(42) + "| MB/s |"+ " Mfloat/s | min ns/f | avg tick/f |");
print_separator();
}
private static void print_separator()
{
Console.WriteLine("-----------------------------------------------------------------------------------------------------");
}
private static void process_test(string[] lines, byte[][] linesUTF8, double volume)
{
print_header();
pretty_print(volume, (uint)lines.Length, "Double.Parse", time_it_ns<double>(lines, find_max_double_parse, 100));
pretty_print(volume, (uint)lines.Length, "FastParser.ParseDouble", time_it_ns<double>(lines, find_max_fast_float, 100));
pretty_print(volume, (uint)lines.Length, "FastParser.TryParseDouble", time_it_ns<double>(lines, find_max_fast_float_try, 100));
pretty_print(volume, (uint)lines.Length, "FastParser.ParseDouble UT8", time_it_ns_ut8<double>(linesUTF8, find_max_fast_float_utf8, 100));
pretty_print(volume, (uint)lines.Length, "FastParser.TryParseDouble UT8", time_it_ns_ut8<double>(linesUTF8, find_max_fast_float_try_utf8, 100));
print_separator();
}
}
}