-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringFormation.cs
62 lines (50 loc) · 1.62 KB
/
StringFormation.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
[MemoryDiagnoser]
[RPlotExporter, RankColumn]
public class StringFormationBenchmark
{
private string _s1;
private string _s2;
private string _s3;
private string _s4;
[Params(10, 50, 100)]
public int N;
[GlobalSetup]
public void Setup()
{
_s1 = RandomString(N);
_s2 = RandomString(N);
_s3 = RandomString(N);
_s4 = RandomString(N);
}
#region 2 variables
[Benchmark]
public string StringConcatenation2Variables() => _s1 + _s2;
[Benchmark]
public string StringFormat2Variables() => string.Format("{0}{1}", _s1, _s2);
[Benchmark]
public string StringInterpolation2Variables() => $"{_s1}{_s2}";
#endregion
#region 3 variables
[Benchmark]
public string StringConcatenation3Variables() => _s1 + _s2 + _s3;
[Benchmark]
public string StringFormat3Variables() => string.Format("{0}{1}{2}", _s1, _s2, _s3);
[Benchmark]
public string StringInterpolation3Variables() => $"{_s1}{_s2}{_s3}";
#endregion
#region 4 variables
[Benchmark]
public string StringConcatenation4Variables() => _s1 + _s2 + _s3 + _s4;
[Benchmark]
public string StringFormat4Variables() => string.Format("{0}{1}{2}{3}", _s1, _s2, _s3, _s4);
[Benchmark]
public string StringInterpolation4Variables() => $"{_s1}{_s2}{_s3}{_s4}";
#endregion
public static string RandomString(int length)
{
var random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
}