Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 23 additions & 114 deletions strings-csharp/ConvertInt2String/ConvertInt2String/Benchmark.cs
Original file line number Diff line number Diff line change
@@ -1,126 +1,35 @@
using System;
using System.Text;
using System.Diagnostics;
using BenchmarkDotNet.Attributes;

namespace ConvertInt2String
{
public static class Benchmark
{
const int formatOffsetLeft = 23;
const int formatOffsetRight = 10;

static string BenchmarkToString(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = num.ToString();
}
watch.Stop();
namespace ConvertInt2String;

return $"|{"'ToString'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
static string BenchmarkConvertToString(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = num.ToString();
}
watch.Stop();

return $"|{"'ConvertToString'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
static string BenchmarkStringFormat(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = string.Format("{0}", num);
}
watch.Stop();
[MemoryDiagnoser]
public class IntToStringBenchmarks
{
[Params(3, 1_234_567)]
public int Number { get; set; }

return $"|{"'String.Format'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
static string BenchmarkStringInterpolation(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = $"{num}";
}
watch.Stop();
[Benchmark(Baseline = true)]
public string IntToString() => Number.ToString();

return $"|{"'String Interpolation'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
static string BenchmarkStringConcat(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = string.Concat(string.Empty, num);
}
watch.Stop();
[Benchmark]
public string ConvertToString() => Convert.ToString(Number);

return $"|{"'String.Concat'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
static string BenchmarkStringJoin(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = string.Join(string.Empty, num);
}
watch.Stop();
[Benchmark]
public string StringFormat() => string.Format("{0}", Number);

return $"|{"'String.Join'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
static string BenchmarkPlusSign(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = string.Empty + num;
}
watch.Stop();
[Benchmark]
public string StringInterpolation() => $"{Number}";

return $"|{"'Plus sign'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
static string BenchmarkStringBuilderAppend(int num, int reps)
{
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < reps; i++)
{
var tmpResult = new StringBuilder().Append(num).ToString();
}
watch.Stop();
[Benchmark]
public string StringConcat() => string.Concat(string.Empty, Number);

return $"|{"'Stringbuilder Append'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
}
[Benchmark]
public string StringJoin() => string.Join(string.Empty, Number);

public static string Run(int number, int repetitions)
{
var benchmarkResultsBuilder = new StringBuilder($"\r\nBenchmarks with {repetitions:N0} repetitions: \r\n")
.AppendLine($"|{"Method",formatOffsetLeft} | {"Speed (ms)",formatOffsetRight}|")
.AppendLine($"|{"-----------------------",formatOffsetLeft}-|-{"----------",formatOffsetRight}|")
.AppendLine(BenchmarkToString(number, repetitions))
.AppendLine(BenchmarkConvertToString(number, repetitions))
.AppendLine(BenchmarkStringFormat(number, repetitions))
.AppendLine(BenchmarkStringInterpolation(number, repetitions))
.AppendLine(BenchmarkStringConcat(number, repetitions))
.AppendLine(BenchmarkStringJoin(number, repetitions))
.AppendLine(BenchmarkPlusSign(number, repetitions))
.AppendLine(BenchmarkStringBuilderAppend(number, repetitions));
[Benchmark]
public string PlusSign() => string.Empty + Number;

return benchmarkResultsBuilder.ToString();
}
}
[Benchmark]
public string StringBuilderAppend() => new StringBuilder().Append(Number).ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
15 changes: 8 additions & 7 deletions strings-csharp/ConvertInt2String/ConvertInt2String/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ConvertInt2String;
using System.Globalization;
using System.Text;
using BenchmarkDotNet.Running;
using ConvertInt2String;

var luckyNumber = 3;

Expand All @@ -14,11 +16,10 @@
Console.WriteLine(luckyNumber + luckyNumber + string.Empty + luckyNumber + luckyNumber);
Console.WriteLine(new StringBuilder().Append(luckyNumber).ToString());

Console.WriteLine(Benchmark.Run(luckyNumber, 200000));
Console.WriteLine(Benchmark.Run(luckyNumber, 2000000));

Console.ReadLine();


var negativeNumber = -5;
var swedish = CultureInfo.GetCultureInfo("sv-SE");

Console.WriteLine(negativeNumber.ToString(swedish) == "-5");
Console.WriteLine(negativeNumber.ToString(CultureInfo.InvariantCulture) == "-5");

BenchmarkSwitcher.FromAssembly(typeof(IntToStringBenchmarks).Assembly).Run(args);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using System.Text;

namespace ConvertInt2StringTests
Expand Down Expand Up @@ -77,5 +78,23 @@ public void GivenInt_WhenConvertedToStringWithStringBuilderAppend_ThenReturnStri

Assert.AreEqual("3", str);
}

[TestMethod]
public void GivenNegativeInt_WhenConvertedWithSwedishCulture_ThenUsesUnicodeMinusSign()
{
var num = -5;
var str = num.ToString(CultureInfo.GetCultureInfo("sv-SE"));

Assert.AreEqual("\u22125", str);
}

[TestMethod]
public void GivenNegativeInt_WhenConvertedWithInvariantCulture_ThenUsesHyphenMinus()
{
var num = -5;
var str = num.ToString(CultureInfo.InvariantCulture);

Assert.AreEqual("-5", str);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

</Project>
Loading