-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
79 lines (63 loc) · 2.02 KB
/
Copy pathProgram.cs
File metadata and controls
79 lines (63 loc) · 2.02 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
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchmark>();
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net70)] // PGO enabled by default
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net90, baseline: true)]
[HideColumns(Column.Job)]
public class Benchmark
{
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string StringCreate_Case(string a, string b, string c)
{
int totalLength = a.Length + 1 + b.Length + 1 + c.Length;
char[] buffer = new char[totalLength];
int pos = 0;
InternalWriteBuffer(ref buffer, ref a, ref pos);
buffer[pos++] = ' ';
InternalWriteBuffer(ref buffer, ref b, ref pos);
buffer[pos++] = ' ';
InternalWriteBuffer(ref buffer, ref c, ref pos);
return new string(buffer);
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string StringJoin_Case(string a, string b, string c)
{
return string.Join(' ', a, b, c);
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ValueStringBuilder_Case(string a, string b, string c)
{
int totalLength = a.Length + 1 + b.Length + 1 + c.Length;
ValueStringBuilder sb = new(totalLength);
sb.Append(a.AsSpan());
sb.Append(' ');
sb.Append(b.AsSpan());
sb.Append(' ');
sb.Append(c.AsSpan());
return sb.ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void InternalWriteBuffer(ref char[] buffer, ref string data, ref int pos)
{
for (int i = 0; i < data.Length; i++)
{
buffer[pos++] = data[i];
}
}
public IEnumerable<object[]> TestData()
{
yield return new object[] { "Mr.", "Benjamin", "Abt" };
}
}