Skip to content

Commit a853894

Browse files
committed
Add StringBuilderPerf
1 parent 78cd587 commit a853894

File tree

6 files changed

+84
-4
lines changed

6 files changed

+84
-4
lines changed

AutoFixtureXUnit/UnitTests.cs

-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ public class UnitTest1
99
[AutoData]
1010
public void TestOne(string name)
1111
{
12-
var sut = new MyFooService();
13-
14-
var result = sut.MyCall(name);
1512

16-
Assert.NotNull(result);
1713
}
1814

1915
[Theory]

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| ---------------------------------------------------------------------------------------- | ------------ |
7+
| ["Use always a StringBuilder" - Internet myths](StringBuilderPerformance/) | 20.11.2022 |
78
| [Anonymous test data with AutoFixture](AutoFixtureXUnit/) | 19.11.2022 |
89
| [LiteDB - A .NET embedded NoSQL database](LiteDatabase/) | 19.10.2022 |
910
| [Cursed C# - Doing shenanigans in C#](CursedCSharp/) | 15.10.2022 |

StringBuilderPerformance/Program.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Text;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Running;
4+
5+
BenchmarkRunner.Run<StringBuilderBenchmark>();
6+
7+
[MemoryDiagnoser]
8+
public class StringBuilderBenchmark
9+
{
10+
private const string TextToAppend = "Hello World from Steven";
11+
12+
[Params(5, 10, 25)]
13+
public int Iterations { get; set; }
14+
15+
[Benchmark]
16+
public string StringBuilder()
17+
{
18+
var builder = new StringBuilder();
19+
for (var i = 0; i < Iterations; i++)
20+
builder.Append(TextToAppend);
21+
22+
return builder.ToString();
23+
}
24+
25+
[Benchmark]
26+
public string ListOfStrings()
27+
{
28+
var list = new List<string>();
29+
for(var i = 0; i< Iterations;i++)
30+
list.Add(TextToAppend);
31+
32+
return string.Join(string.Empty, list);
33+
}
34+
35+
[Benchmark]
36+
public string Concat()
37+
{
38+
var output = string.Empty;
39+
for (var i = 0; i < Iterations; i++)
40+
output += TextToAppend;
41+
42+
return output;
43+
}
44+
}

StringBuilderPerformance/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# "Use always a StringBuilder" - Internet myths
2+
3+
> Use always a `StringBuilder`
4+
5+
That is what you can read from time to time. The basic idea is, that a `StringBuilder` is "better" ^to be defined^.
6+
7+
Why are people telling that ^lie^? Let's discuss this and see what it isn't true.
8+
9+
Found [here](https://steven-giesel.com/blogPost/480539f1-98ab-45bc-ba24-9ccec65b459a)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
12+
</ItemGroup>
13+
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringBuilderPerformance", "StringBuilderPerformance.csproj", "{1098C5A1-15C2-4567-AFB5-6668173E4C39}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{1098C5A1-15C2-4567-AFB5-6668173E4C39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{1098C5A1-15C2-4567-AFB5-6668173E4C39}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{1098C5A1-15C2-4567-AFB5-6668173E4C39}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{1098C5A1-15C2-4567-AFB5-6668173E4C39}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

0 commit comments

Comments
 (0)