-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
135 lines (112 loc) · 3.39 KB
/
Copy pathProgram.cs
File metadata and controls
135 lines (112 loc) · 3.39 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
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System;
using System.Buffers;
using System.Text;
using System.Text.RegularExpressions;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchmark>();
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net90, baseline: true)]
[HideColumns(Column.Job)]
public class Benchmark
{
public const string Input = @"""
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
Hello\u0001World Hello\u0001World Hello\u0001World Hello\u0001World
""";
[Benchmark]
public string Regex()
{
return RegexSample.WhiteSpaceRegex().Replace(Input, "");
}
[Benchmark]
public string String()
{
string data = Input;
return data.Replace(" ", "");
}
[Benchmark]
public string Span()
{
ReadOnlySpan<char> inputSpan = Input.AsSpan();
Span<char> resultSpan = stackalloc char[Input.Length];
int resultIndex = 0;
foreach (char c in inputSpan)
{
if (c is not ' ')
{
resultSpan[resultIndex++] = c;
}
}
return new string(resultSpan.Slice(0, resultIndex));
}
[Benchmark]
public string StringBuilder()
{
StringBuilder stringBuilder = new(Input);
stringBuilder.Replace(" ", "");
return stringBuilder.ToString();
}
[Benchmark]
public string JoinSplit()
{
return string.Join("", Input.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
}
[Benchmark]
public string ConcatSplit()
{
return string.Concat(Input.Split(null));
}
[Benchmark]
public string SpanArrayPool()
{
char[] pooledArray = ArrayPool<char>.Shared.Rent(Input.Length);
try
{
Span<char> destination = pooledArray.AsSpan(0, Input.Length);
int pos = 0;
foreach (char c in Input)
{
if (!char.IsWhiteSpace(c))
{
destination[pos++] = c;
}
}
return Input.Length == pos ? Input : new string(destination[..pos]);
}
finally
{
ArrayPool<char>.Shared.Return(pooledArray);
}
}
[Benchmark]
public string SpanStackPool()
{
// this only works when Input <256 to avoid heap allocation
Span<char> destination = stackalloc char[Input.Length];
int pos = 0;
foreach (char c in Input)
{
if (!char.IsWhiteSpace(c))
{
destination[pos++] = c;
}
}
return Input.Length == pos ? Input : new string(destination[..pos]);
}
}
public static partial class RegexSample
{
[GeneratedRegex(@"\s+")]
public static partial Regex WhiteSpaceRegex();
}