-
-
Save dezhidki/73b07e8a636e5792ea89b221a5b015ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
BenchmarkRunner.Run<SBBench>(); | |
[MemoryDiagnoser] | |
public class SBBench | |
{ | |
private const int N = 10000; | |
[Benchmark] | |
public string SBTestAdd() | |
{ | |
var sb = new StringBuilder("a"); | |
for (var i = 0; i < N; i++) | |
sb.Append("a" + i); | |
return sb.ToString(); | |
} | |
[Benchmark] | |
public string SBTestAppend() | |
{ | |
var sb = new StringBuilder("a"); | |
for (var i = 0; i < N; i++) | |
{ | |
sb.Append("a"); | |
sb.Append(i); | |
} | |
return sb.ToString(); | |
} | |
[Benchmark] | |
public string SBTestConcat() | |
{ | |
var s = ""; | |
for (var i = 0; i < N; i++) | |
{ | |
s += "a" + i; | |
} | |
return s; | |
} | |
[Benchmark] | |
public string SBTestFormat() | |
{ | |
var s = ""; | |
for (var i = 0; i < N; i++) | |
{ | |
s += $"a{i}"; | |
} | |
return s; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment