-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (100 loc) · 3.24 KB
/
Copy pathProgram.cs
File metadata and controls
120 lines (100 loc) · 3.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
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
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System;
using System.Linq;
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
{
[Params(10, 100, 1000)]
public int CharLength { get; set; }
[Benchmark]
public string StringCreate()
=> StringCreateSample.CreateRandomString(CharLength);
[Benchmark]
public string EnumerateRepeat()
=> EnumerateRepeatSample.CreateRandomString(CharLength);
[Benchmark]
public string CharArray()
=> CharArraySample.CreateRandomString(CharLength);
[Benchmark]
public string Span()
=> SpanSample.CreateRandomString(CharLength);
}
public static class SampleConstants
{
public const string UpperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const string LowerChars = "abcdefghijklmnopqrstuvwxyz";
public const string Digits = "0123456789";
public const string AlphNum = UpperChars + LowerChars + Digits;
}
public static class StringCreateSample
{
private static readonly Random _random = new();
private static void CreateRandomString(Span<char> buffer)
{
const string chars = SampleConstants.AlphNum;
int charsLength = chars.Length;
for (int i = 0; i < buffer.Length; ++i)
{
int cl = charsLength;
buffer[i] = chars[_random.Next(cl)];
}
}
public static string CreateRandomString(int length)
{
return string.Create<object>(length, null, static (buffer, _) => CreateRandomString(buffer));
}
}
public static class SpanSample
{
private static readonly Random _random = new();
private static void CreateRandomString(Span<char> buffer)
{
const string chars = SampleConstants.AlphNum;
ReadOnlySpan<char> charsSpan = chars.AsSpan();
int charsLength = chars.Length;
for (int i = 0; i < buffer.Length; ++i)
{
int cl = charsLength;
buffer[i] = charsSpan[_random.Next(cl)];
}
}
public static string CreateRandomString(int length)
{
return string.Create<object>(length, null, static (buffer, _) => CreateRandomString(buffer));
}
}
public static class EnumerateRepeatSample
{
private static readonly Random _random = new();
public static string CreateRandomString(int length)
{
const string chars = SampleConstants.AlphNum;
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[_random.Next(s.Length)]).ToArray());
}
}
public static class CharArraySample
{
private static readonly Random _random = new();
public static string CreateRandomString(int length)
{
const string chars = SampleConstants.AlphNum;
int charsLength = chars.Length;
char[] charArray = new char[length];
for (int i = 0; i < charArray.Length; i++)
{
int cl = charsLength;
charArray[i] = chars[_random.Next(cl)];
}
return new(charArray);
}
}