-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
104 lines (82 loc) · 2.01 KB
/
Copy pathProgram.cs
File metadata and controls
104 lines (82 loc) · 2.01 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
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Microsoft.Extensions.ObjectPool;
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
{
private List<string> _data;
[Params(100, 500)]
public int Lines { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
_data = Enumerable.Range(0, Lines)
.Select(x => new string('a', x)).ToList();
_sbPool = ObjectPool.Create<StringBuilder>();
}
private ObjectPool<StringBuilder> _sbPool;
[Benchmark(Baseline = true)]
public string SB_Pooled()
{
// retrive from pool
StringBuilder sb = _sbPool.Get();
foreach (string entry in _data)
{
string e = entry;
sb.Append(e);
}
string s = sb.ToString();
// cleanup and return to pool
sb.Clear();
_sbPool.Return(sb);
return s;
}
[Benchmark]
public string SB_NoPool()
{
StringBuilder sb = new();
foreach (string entry in _data)
{
string e = entry;
sb.Append(e);
}
return sb.ToString();
}
[Benchmark]
public string ConcatLong()
{
string s = string.Empty;
foreach (string entry in _data)
{
s = s + entry;
}
return s;
}
[Benchmark]
public string ConcatShort()
{
string s = string.Empty;
foreach (string entry in _data)
{
s += entry;
}
return s;
}
[Benchmark]
public string ConcatList()
{
string s = string.Concat(_data);
return s;
}
}