-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFastParserBenchmark.cs
150 lines (109 loc) · 2.89 KB
/
FastParserBenchmark.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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
//using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using csFastFloat.Structures;
using System;
using System.Globalization;
using BenchmarkDotNet.Reports;
using System.Collections.Generic;
namespace csFastFloat.Benchmark
{
//[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net90)]
[Config(typeof(Config))]
public class FFBenchmark
{
private string[] _lines;
private byte[][] _linesUtf8;
private class Config : ManualConfig
{
public Config()
{
AddColumn(StatisticColumn.Min);
AddColumn(new MFloatPerSecColumn());
AddColumn(new VolumePerSecColumn());
}
}
[Benchmark(Description = "Utf8Parser")]
public double Utf8Parser()
{
double max = double.MinValue;
foreach (byte[] l in _linesUtf8)
{
if (!System.Buffers.Text.Utf8Parser.TryParse(l, out double d, out int consumed) || consumed != l.Length)
throw new InvalidOperationException();
max = d > max ? d : max;
}
return max;
}
[Benchmark(Description = "FastFloat.TryParseDouble() - UTF8")]
public double FastParserUtf8_()
{
double max = double.MinValue;
foreach (byte[] l in _linesUtf8)
{
FastDoubleParser.TryParseDouble(l, out double d);
max = d > max ? d : max;
}
return max;
}
[Benchmark(Description = "FastFloat.TryParseDouble()")]
public double FastParser_()
{
double max = double.MinValue;
foreach (string l in _lines)
{
FastDoubleParser.TryParseDouble(l, out double d);
max = d > max ? d : max;
}
return max;
}
[Benchmark(Description = "ParseNumberString() only")]
public double FastParser_PNS()
{
double max = double.MinValue;
foreach (string l in _lines)
{
unsafe {
fixed (char* p = l)
{
var pni = ParsedNumberString.ParseNumberString(p, p + l.Length);
max = pni.exponent > max ? pni.exponent: max;
}
}
}
return max;
}
[Benchmark(Baseline = true, Description = "Double.Parse()")]
public double Double_std()
{
double max = double.MinValue;
foreach (string l in _lines)
{
double d = double.Parse(l, CultureInfo.InvariantCulture);
max = d > max ? d : max;
}
return max;
}
[Params(@"data/canada.txt", @"data/mesh.txt", @"data/synthetic.txt")]
public string FileName;
[GlobalSetup]
public void Setup()
{
Console.WriteLine("reading data");
_lines = System.IO.File.ReadAllLines(FileName);
_linesUtf8 = Array.ConvertAll(_lines, System.Text.Encoding.UTF8.GetBytes);
}
}
public class Program
{
public static void Main(string[] args)
{
var config = DefaultConfig.Instance.WithSummaryStyle( SummaryStyle.Default.WithMaxParameterColumnWidth(100));
BenchmarkRunner.Run<FFBenchmark>(config);
}
}
}