-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathCountTokens.cs
100 lines (90 loc) · 3.18 KB
/
CountTokens.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
using System.IO;
using System.Text.Json;
using BenchmarkDotNet.Attributes;
using Newtonsoft.Json;
using SimdJsonSharp;
namespace Benchmarks
{
public class CountTokens : BenchmarksBase
{
[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(TestData))]
public unsafe ulong _SimdJson(byte[] data, string fileName, string fileSize)
{
ulong numbersCount = 0;
fixed (byte* dataPtr = data)
{
using (ParsedJson doc = SimdJson.ParseJson(dataPtr, data.Length))
using (var iterator = new ParsedJsonIterator(doc))
{
while (iterator.MoveForward())
if (iterator.IsDouble || iterator.IsInteger)
numbersCount++;
}
}
return numbersCount;
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public unsafe ulong _SimdJsonN(byte[] data, string fileName, string fileSize)
{
ulong numbersCount = 0;
fixed (byte* dataPtr = data)
{
using (ParsedJsonN doc = SimdJsonN.ParseJson(dataPtr, data.Length))
using (var iterator = new ParsedJsonIteratorN(doc))
{
while (iterator.MoveForward())
//if (iterator.IsDouble || iterator.IsInteger)
numbersCount++;
}
}
return numbersCount;
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public ulong _Utf8JsonReader(byte[] data, string fileName, string fileSize)
{
ulong numbersCount = 0;
var reader = new Utf8JsonReader(data, true, default);
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.Number)
numbersCount++;
}
return numbersCount;
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public ulong JsonNet(byte[] data, string fileName, string fileSize)
{
ulong numbersCount = 0;
using (var streamReader = new StreamReader(new MemoryStream(data)))
{
JsonTextReader reader = new JsonTextReader(streamReader);
while (reader.Read())
{
if (reader.TokenType == JsonToken.Float ||
reader.TokenType == JsonToken.Integer)
numbersCount++;
}
}
return numbersCount;
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public ulong SpanJsonUtf8(byte[] data, string fileName, string fileSize)
{
ulong numbersCount = 0;
var reader = new SpanJson.JsonReader<byte>(data);
SpanJson.JsonToken token;
while ((token = reader.ReadUtf8NextToken()) != SpanJson.JsonToken.None)
{
if (token == SpanJson.JsonToken.Number)
numbersCount++;
reader.SkipNextUtf8Value(token);
}
return numbersCount;
}
}
}