/
StringBenchmark.cs
231 lines (180 loc) · 7.61 KB
/
StringBenchmark.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Mathematics;
using BenchmarkDotNet.Order;
using StringHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Benchmark
{
[RankColumn, HtmlExporter, CsvExporter]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class StringBenchmark
{
private DateTime _dt = DateTime.Now;
//#region Normal
//[Benchmark]
//public string StringBuilder()
//{
// System.Text.StringBuilder m_strBuilder = new System.Text.StringBuilder(1);
// m_strBuilder.Append("PI=").Append("_373=").Append(373).Append(true).Append(short.MaxValue).Append('z').Replace("373", "5428").Replace("St Paul", "HOT");
// return m_strBuilder.ToString();
//}
//[Benchmark]
//public string LiteStringBuilder()
//{
// LiteStringBuilder m_strCustom = new LiteStringBuilder(1);
// m_strCustom.Append("PI=").Append("_373=").Append(373).Append(true).Append(short.MaxValue).Append('z').Replace("373", "5428").Replace("St Paul", "HOT");
// return m_strCustom.ToString();
//}
//#endregion
#region Normal
[Benchmark]
public string String_Interpolated()
{
string str = $"PI= { Math.PI} _373= { 373 } {true} {short.MaxValue}{'z'}";
return str.Replace("373", "5428");
}
[Benchmark]
public string String_Added()
{
string str = "PI=" + Math.PI + "_373=" + 373.ToString() + true.ToString() + short.MaxValue.ToString() + 'z';
return str.Replace("373", "5428").Replace("St Paul", "HOT");
}
[Benchmark]
public string String_Concat()
{
string str = string.Concat("PI=", Math.PI, "_373=", 373, true, short.MaxValue, 'z');
return str.Replace("373", "5428").Replace("St Paul", "HOT");
}
[Benchmark]
public string StringBuilder()
{
System.Text.StringBuilder m_strBuilder = new System.Text.StringBuilder(1);
m_strBuilder.Append("PI=").Append(Math.PI).Append("_373=").Append(373).Append(true).Append(short.MaxValue).Append('z').Replace("373", "5428").Replace("St Paul", "HOT");
return m_strBuilder.ToString();
}
[Benchmark]
public string LiteStringBuilder()
{
var m_strBuilder = new LiteStringBuilder(1);
m_strBuilder.Append("PI=").Append(Math.PI).Append("_373=").Append(373).Append(true).Append(short.MaxValue).Append('z').Replace("373", "5428").Replace("St Paul", "HOT");
return m_strBuilder.ToString();
}
#endregion
#region BIGString
private readonly static string str1 = new string('a', 1000);
private readonly static string str2 = new string('b', 1000);
private readonly static string str3 = new string('c', 1000);
private readonly static string str4 = new string('d', 1000);
[Benchmark]
public string Large_String_Interpolated()
{
string str = $"{str1} {str2}{str3}{str4}";
return str.Replace("c", "z");
}
[Benchmark]
public string Large_String_Added()
{
string str = str1 + str2 + str3 + str4;
return str.Replace("c", "z");
}
[Benchmark]
public string Large_String_Concat()
{
string str = string.Concat(str1, str2, str3, str4);
return str.Replace("c", "z");
}
[Benchmark]
public string Large_StringBuilder()
{
System.Text.StringBuilder m_strBuilder = new System.Text.StringBuilder(1);
//m_strBuilder.Length = 0;
m_strBuilder.Append(str1).Append(str2).Append(str3).Append(str4);
return m_strBuilder.ToString();
}
[Benchmark]
public string Large_LiteStringBuilder()
{
LiteStringBuilder m_strCustom = new LiteStringBuilder(1);
// m_strCustom.Clear();
m_strCustom.Append(str1).Append(str2).Append(str3).Append(str4);
return m_strCustom.ToString();
}
#endregion
#region BIGArray
private readonly static char[] _bigArray1;
private readonly static char[] _bigArray2;
private readonly static char[] _bigArray3;
static StringBenchmark()
{
_bigArray1 = new char[char.MaxValue];
_bigArray2 = new char[char.MaxValue];
_bigArray3 = new char[char.MaxValue];
for (var i = 0; i < char.MaxValue; i++)
{
_bigArray1[i] = (char)i;
_bigArray2[i] = (char)i;
_bigArray3[i] = (char)i;
}
}
[Benchmark]
public string LargeArray_StringBuilder()
{
System.Text.StringBuilder m_strBuilder = new System.Text.StringBuilder(1);
//m_strBuilder.Length = 0;
m_strBuilder.Append(_bigArray1).Append(_bigArray2).Append(_bigArray3);
return m_strBuilder.ToString();
}
[Benchmark]
public string LargeArray_LiteStringBuilder()
{
LiteStringBuilder m_strCustom = new LiteStringBuilder(1);
// m_strCustom.Clear();
m_strCustom.Append(_bigArray1).Append(_bigArray2).Append(_bigArray3);
return m_strCustom.ToString();
}
#endregion
#region PrimativeTypes
[Benchmark]
public string Primative_String_Interpolated()
{
string str = $"{char.MaxValue} {Int16.MaxValue}{Int32.MaxValue}{Int64.MaxValue}{DateTime.MaxValue}{double.MaxValue}{decimal.MaxValue}{float.MaxValue}{true}{byte.MaxValue}{sbyte.MaxValue}";
return str;
}
[Benchmark]
public string Primative_String_Added()
{
string str = char.MaxValue + Int16.MaxValue.ToString() + Int32.MaxValue.ToString() + Int64.MaxValue.ToString() + DateTime.MaxValue.ToString() + double.MaxValue.ToString() + decimal.MaxValue.ToString() + float.MaxValue.ToString() + true.ToString() + byte.MaxValue.ToString() + sbyte.MaxValue.ToString();
return str;
}
[Benchmark]
public string Primative_String_Concat()
{
string str = string.Concat(char.MaxValue, Int16.MaxValue, Int32.MaxValue, Int64.MaxValue, DateTime.MaxValue, double.MaxValue, float.MaxValue, true, byte.MaxValue, sbyte.MaxValue);
return str.Replace("c", "z");
}
[Benchmark]
public string Primative_StringBuilder()
{
System.Text.StringBuilder m_strBuilder = new System.Text.StringBuilder(64);
//m_strBuilder.Length = 0;
m_strBuilder.Append(char.MaxValue).Append(Int16.MaxValue).Append(Int32.MaxValue).Append(Int64.MaxValue).Append(DateTime.MaxValue).Append(double.MaxValue).Append(float.MaxValue).Append(true).Append(byte.MaxValue).Append(sbyte.MaxValue);
return m_strBuilder.ToString();
}
[Benchmark]
public string Primative_LiteStringBuilder()
{
LiteStringBuilder m_strCustom = new LiteStringBuilder(64);
// m_strCustom.Clear();
m_strCustom.Append(char.MaxValue).Append(Int16.MaxValue).Append(Int32.MaxValue).Append(Int64.MaxValue).Append(DateTime.MaxValue).Append(double.MaxValue).Append(float.MaxValue).Append(true).Append(byte.MaxValue).Append(sbyte.MaxValue);
return m_strCustom.ToString();
}
#endregion
}
}