Skip to content

Commit d2c6d0b

Browse files
ConvertInt2String: move to net10.0, BenchmarkDotNet harness, culture tests (#2325)
Retarget both projects to net10.0, update MSTest to 4.5.1 and the test SDK and coverlet to current releases, replace the Stopwatch benchmark (whose ConvertToString case measured ToString) with a BenchmarkDotNet class run through BenchmarkSwitcher, and add two tests for the negative sign under sv-SE and the invariant culture.
1 parent cbc0ec0 commit d2c6d0b

5 files changed

Lines changed: 61 additions & 128 deletions

File tree

Lines changed: 23 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,35 @@
1-
using System;
21
using System.Text;
3-
using System.Diagnostics;
2+
using BenchmarkDotNet.Attributes;
43

5-
namespace ConvertInt2String
6-
{
7-
public static class Benchmark
8-
{
9-
const int formatOffsetLeft = 23;
10-
const int formatOffsetRight = 10;
11-
12-
static string BenchmarkToString(int num, int reps)
13-
{
14-
var watch = new Stopwatch();
15-
watch.Start();
16-
for (int i = 0; i < reps; i++)
17-
{
18-
var tmpResult = num.ToString();
19-
}
20-
watch.Stop();
4+
namespace ConvertInt2String;
215

22-
return $"|{"'ToString'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
23-
}
24-
static string BenchmarkConvertToString(int num, int reps)
25-
{
26-
var watch = new Stopwatch();
27-
watch.Start();
28-
for (int i = 0; i < reps; i++)
29-
{
30-
var tmpResult = num.ToString();
31-
}
32-
watch.Stop();
33-
34-
return $"|{"'ConvertToString'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
35-
}
36-
static string BenchmarkStringFormat(int num, int reps)
37-
{
38-
var watch = new Stopwatch();
39-
watch.Start();
40-
for (int i = 0; i < reps; i++)
41-
{
42-
var tmpResult = string.Format("{0}", num);
43-
}
44-
watch.Stop();
6+
[MemoryDiagnoser]
7+
public class IntToStringBenchmarks
8+
{
9+
[Params(3, 1_234_567)]
10+
public int Number { get; set; }
4511

46-
return $"|{"'String.Format'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
47-
}
48-
static string BenchmarkStringInterpolation(int num, int reps)
49-
{
50-
var watch = new Stopwatch();
51-
watch.Start();
52-
for (int i = 0; i < reps; i++)
53-
{
54-
var tmpResult = $"{num}";
55-
}
56-
watch.Stop();
12+
[Benchmark(Baseline = true)]
13+
public string IntToString() => Number.ToString();
5714

58-
return $"|{"'String Interpolation'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
59-
}
60-
static string BenchmarkStringConcat(int num, int reps)
61-
{
62-
var watch = new Stopwatch();
63-
watch.Start();
64-
for (int i = 0; i < reps; i++)
65-
{
66-
var tmpResult = string.Concat(string.Empty, num);
67-
}
68-
watch.Stop();
15+
[Benchmark]
16+
public string ConvertToString() => Convert.ToString(Number);
6917

70-
return $"|{"'String.Concat'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
71-
}
72-
static string BenchmarkStringJoin(int num, int reps)
73-
{
74-
var watch = new Stopwatch();
75-
watch.Start();
76-
for (int i = 0; i < reps; i++)
77-
{
78-
var tmpResult = string.Join(string.Empty, num);
79-
}
80-
watch.Stop();
18+
[Benchmark]
19+
public string StringFormat() => string.Format("{0}", Number);
8120

82-
return $"|{"'String.Join'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
83-
}
84-
static string BenchmarkPlusSign(int num, int reps)
85-
{
86-
var watch = new Stopwatch();
87-
watch.Start();
88-
for (int i = 0; i < reps; i++)
89-
{
90-
var tmpResult = string.Empty + num;
91-
}
92-
watch.Stop();
21+
[Benchmark]
22+
public string StringInterpolation() => $"{Number}";
9323

94-
return $"|{"'Plus sign'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|";
95-
}
96-
static string BenchmarkStringBuilderAppend(int num, int reps)
97-
{
98-
var watch = new Stopwatch();
99-
watch.Start();
100-
for (int i = 0; i < reps; i++)
101-
{
102-
var tmpResult = new StringBuilder().Append(num).ToString();
103-
}
104-
watch.Stop();
24+
[Benchmark]
25+
public string StringConcat() => string.Concat(string.Empty, Number);
10526

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

109-
public static string Run(int number, int repetitions)
110-
{
111-
var benchmarkResultsBuilder = new StringBuilder($"\r\nBenchmarks with {repetitions:N0} repetitions: \r\n")
112-
.AppendLine($"|{"Method",formatOffsetLeft} | {"Speed (ms)",formatOffsetRight}|")
113-
.AppendLine($"|{"-----------------------",formatOffsetLeft}-|-{"----------",formatOffsetRight}|")
114-
.AppendLine(BenchmarkToString(number, repetitions))
115-
.AppendLine(BenchmarkConvertToString(number, repetitions))
116-
.AppendLine(BenchmarkStringFormat(number, repetitions))
117-
.AppendLine(BenchmarkStringInterpolation(number, repetitions))
118-
.AppendLine(BenchmarkStringConcat(number, repetitions))
119-
.AppendLine(BenchmarkStringJoin(number, repetitions))
120-
.AppendLine(BenchmarkPlusSign(number, repetitions))
121-
.AppendLine(BenchmarkStringBuilderAppend(number, repetitions));
30+
[Benchmark]
31+
public string PlusSign() => string.Empty + Number;
12232

123-
return benchmarkResultsBuilder.ToString();
124-
}
125-
}
33+
[Benchmark]
34+
public string StringBuilderAppend() => new StringBuilder().Append(Number).ToString();
12635
}

‎strings-csharp/ConvertInt2String/ConvertInt2String/ConvertInt2String.csproj‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
12+
</ItemGroup>
13+
1014
</Project>
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using ConvertInt2String;
1+
using System.Globalization;
22
using System.Text;
3+
using BenchmarkDotNet.Running;
4+
using ConvertInt2String;
35

46
var luckyNumber = 3;
57

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

17-
Console.WriteLine(Benchmark.Run(luckyNumber, 200000));
18-
Console.WriteLine(Benchmark.Run(luckyNumber, 2000000));
19-
20-
Console.ReadLine();
21-
22-
19+
var negativeNumber = -5;
20+
var swedish = CultureInfo.GetCultureInfo("sv-SE");
2321

22+
Console.WriteLine(negativeNumber.ToString(swedish) == "-5");
23+
Console.WriteLine(negativeNumber.ToString(CultureInfo.InvariantCulture) == "-5");
2424

25+
BenchmarkSwitcher.FromAssembly(typeof(IntToStringBenchmarks).Assembly).Run(args);

‎strings-csharp/ConvertInt2String/ConvertInt2StringTests/ConvertInt2StringTests.cs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System.Globalization;
23
using System.Text;
34

45
namespace ConvertInt2StringTests
@@ -77,5 +78,23 @@ public void GivenInt_WhenConvertedToStringWithStringBuilderAppend_ThenReturnStri
7778

7879
Assert.AreEqual("3", str);
7980
}
81+
82+
[TestMethod]
83+
public void GivenNegativeInt_WhenConvertedWithSwedishCulture_ThenUsesUnicodeMinusSign()
84+
{
85+
var num = -5;
86+
var str = num.ToString(CultureInfo.GetCultureInfo("sv-SE"));
87+
88+
Assert.AreEqual("\u22125", str);
89+
}
90+
91+
[TestMethod]
92+
public void GivenNegativeInt_WhenConvertedWithInvariantCulture_ThenUsesHyphenMinus()
93+
{
94+
var num = -5;
95+
var str = num.ToString(CultureInfo.InvariantCulture);
96+
97+
Assert.AreEqual("-5", str);
98+
}
8099
}
81-
}
100+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66

77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13-
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
14-
<PackageReference Include="coverlet.collector" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
13+
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
14+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
1515
</ItemGroup>
1616

1717
</Project>

0 commit comments

Comments
 (0)