-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBenchmarks.cs
More file actions
49 lines (40 loc) · 1.24 KB
/
StringBenchmarks.cs
File metadata and controls
49 lines (40 loc) · 1.24 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
using BenchmarkDotNet.Attributes;
namespace Benchmark
{
[MemoryDiagnoser]
public class StringBenchmarks
{
private const string INPUT = "MAXIME-Hello World";
[Benchmark]
public bool StarstWith()
{
string comparisonString = "MAX";
return INPUT.StartsWith(comparisonString);
}
[Benchmark]
public bool StartsWith_Span()
{
ReadOnlySpan<char> inputSpan = INPUT.AsSpan();
Span<char> comparisonSpan = new (new[] { 'M', 'A', 'X' });
for (int i = 0; i < comparisonSpan.Length; i++)
{
if (comparisonSpan[i] != inputSpan[i])
return false;
}
return true;
//return inputSpan.Slice(0, 3) == comparisonSpan;
}
[Benchmark]
public bool StartsWith_SpanStackAlloc()
{
ReadOnlySpan<char> inputSpan = INPUT.AsSpan();
Span<char> comparisonSpan = stackalloc char[] { 'M', 'A', 'X'};
for (int i = 0; i < comparisonSpan.Length; i++)
{
if (comparisonSpan[i] != inputSpan[i])
return false;
}
return true;
}
}
}