Skip to content

Commit 51be470

Browse files
committed
Add benchmark tests for ToString
(cherry picked from commit 81808e5)
1 parent 4738eed commit 51be470

File tree

9 files changed

+525
-0
lines changed

9 files changed

+525
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36623.8
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.CoreLibrary.Benchmarks", "nanoFramework.CoreLibrary.Benchmarks\nanoFramework.CoreLibrary.Benchmarks.nfproj", "{DFF849AB-F56C-4173-9E3C-5FD551914B0F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{DFF849AB-F56C-4173-9E3C-5FD551914B0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DFF849AB-F56C-4173-9E3C-5FD551914B0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DFF849AB-F56C-4173-9E3C-5FD551914B0F}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{DFF849AB-F56C-4173-9E3C-5FD551914B0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{DFF849AB-F56C-4173-9E3C-5FD551914B0F}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{DFF849AB-F56C-4173-9E3C-5FD551914B0F}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {B0D58340-A787-48A6-B53F-1678A4E57960}
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics;
5+
using System.Threading;
6+
using nanoFramework.Benchmark;
7+
using nanoFramework.CoreLibrary.Benchmarks.ToString;
8+
using nanoFramework.Runtime.Native;
9+
10+
namespace nanoFramework.CoreLibrary.Benchmarks
11+
{
12+
public class Program
13+
{
14+
public static void Main()
15+
{
16+
// Display header with system information
17+
Debug.WriteLine("================================================");
18+
Debug.WriteLine(" nanoFramework Core Library Benchmarks");
19+
Debug.WriteLine("================================================");
20+
Debug.WriteLine("");
21+
22+
Debug.WriteLine($"Target Name: {SystemInfo.TargetName}");
23+
Debug.WriteLine($"Firmware: {SystemInfo.Version}");
24+
var coreLibVersion = typeof(object).Assembly.GetName().Version;
25+
Debug.WriteLine($"CoreLibrary Version: {coreLibVersion}");
26+
27+
Debug.WriteLine("");
28+
Debug.WriteLine("================================================");
29+
Debug.WriteLine("");
30+
31+
// Run benchmarks
32+
BenchmarkRunner.RunClass(typeof(ToStringPlain));
33+
BenchmarkRunner.RunClass(typeof(ToStringConcatenation));
34+
BenchmarkRunner.RunClass(typeof(ToStringInterpolation));
35+
BenchmarkRunner.RunClass(typeof(ToStringFormat));
36+
37+
Thread.Sleep(Timeout.Infinite);
38+
}
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Reflection;
5+
using System.Runtime.CompilerServices;
6+
using System.Runtime.InteropServices;
7+
8+
// General Information about an assembly is controlled through the following
9+
// set of attributes. Change these attribute values to modify the information
10+
// associated with an assembly.
11+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
12+
[assembly: AssemblyDescription("")]
13+
[assembly: AssemblyConfiguration("")]
14+
[assembly: AssemblyCompany("")]
15+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
16+
[assembly: AssemblyCopyright("Copyright © 2025")]
17+
[assembly: AssemblyTrademark("")]
18+
[assembly: AssemblyCulture("")]
19+
20+
// Setting ComVisible to false makes the types in this assembly not visible
21+
// to COM components. If you need to access a type in this assembly from
22+
// COM, set the ComVisible attribute to true on that type.
23+
[assembly: ComVisible(false)]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using nanoFramework.Benchmark;
6+
using nanoFramework.Benchmark.Attributes;
7+
8+
namespace nanoFramework.CoreLibrary.Benchmarks.ToString
9+
{
10+
[IterationCount(5)]
11+
public class ToStringConcatenation
12+
{
13+
private int _intValue;
14+
private long _longValue;
15+
private double _doubleValue;
16+
private bool _boolValue;
17+
private byte _byteValue;
18+
private DateTime _dateTimeValue;
19+
private string _stringValue;
20+
21+
[Setup]
22+
public void Setup()
23+
{
24+
_intValue = 12345;
25+
_longValue = 1234567890123456L;
26+
_doubleValue = 123.456;
27+
_boolValue = true;
28+
_byteValue = 255;
29+
_dateTimeValue = new DateTime(2024, 1, 15, 10, 30, 45);
30+
_stringValue = "test";
31+
}
32+
33+
[Benchmark]
34+
public void Int32Concatenation()
35+
{
36+
_ = "Value: " + _intValue;
37+
}
38+
39+
[Benchmark]
40+
public void Int64Concatenation()
41+
{
42+
_ = "Value: " + _longValue;
43+
}
44+
45+
[Benchmark]
46+
public void DoubleConcatenation()
47+
{
48+
_ = "Value: " + _doubleValue;
49+
}
50+
51+
[Benchmark]
52+
public void BooleanConcatenation()
53+
{
54+
_ = "Value: " + _boolValue;
55+
}
56+
57+
[Benchmark]
58+
public void ByteConcatenation()
59+
{
60+
_ = "Value: " + _byteValue;
61+
}
62+
63+
[Benchmark]
64+
public void DateTimeConcatenation()
65+
{
66+
_ = "Value: " + _dateTimeValue;
67+
}
68+
69+
[Benchmark]
70+
public void MultipleValuesConcatenation()
71+
{
72+
_ = "Int: " + _intValue + ", Double: " + _doubleValue + ", Bool: " + _boolValue;
73+
}
74+
75+
[Benchmark]
76+
public void MixedConcatenation()
77+
{
78+
_ = "String: " + _stringValue + ", Int: " + _intValue + ", DateTime: " + _dateTimeValue;
79+
}
80+
81+
[Benchmark]
82+
public void ComplexConcatenation()
83+
{
84+
_ = "Values - Byte: " + _byteValue + ", Long: " + _longValue + ", Double: " + _doubleValue + ", Bool: " + _boolValue;
85+
}
86+
87+
[Benchmark]
88+
public void ExplicitToStringConcatenation()
89+
{
90+
_ = "Value: " + _intValue.ToString();
91+
}
92+
93+
[Benchmark]
94+
public void MultipleExplicitToStringConcatenation()
95+
{
96+
_ = "Int: " + _intValue.ToString() + ", Double: " + _doubleValue.ToString() + ", Bool: " + _boolValue.ToString();
97+
}
98+
}
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using nanoFramework.Benchmark;
6+
using nanoFramework.Benchmark.Attributes;
7+
8+
namespace nanoFramework.CoreLibrary.Benchmarks.ToString
9+
{
10+
[IterationCount(5)]
11+
public class ToStringFormat
12+
{
13+
private int _intValue;
14+
private long _longValue;
15+
private double _doubleValue;
16+
private bool _boolValue;
17+
private byte _byteValue;
18+
private DateTime _dateTimeValue;
19+
private string _stringValue;
20+
21+
[Setup]
22+
public void Setup()
23+
{
24+
_intValue = 12345;
25+
_longValue = 1234567890123456L;
26+
_doubleValue = 123.456;
27+
_boolValue = true;
28+
_byteValue = 255;
29+
_dateTimeValue = new DateTime(2024, 1, 15, 10, 30, 45);
30+
_stringValue = "test";
31+
}
32+
33+
[Benchmark]
34+
public void Int32Format()
35+
{
36+
_ = string.Format("Value: {0}", _intValue);
37+
}
38+
39+
[Benchmark]
40+
public void Int64Format()
41+
{
42+
_ = string.Format("Value: {0}", _longValue);
43+
}
44+
45+
[Benchmark]
46+
public void DoubleFormat()
47+
{
48+
_ = string.Format("Value: {0}", _doubleValue);
49+
}
50+
51+
[Benchmark]
52+
public void BooleanFormat()
53+
{
54+
_ = string.Format("Value: {0}", _boolValue);
55+
}
56+
57+
[Benchmark]
58+
public void ByteFormat()
59+
{
60+
_ = string.Format("Value: {0}", _byteValue);
61+
}
62+
63+
[Benchmark]
64+
public void DateTimeFormat()
65+
{
66+
_ = string.Format("Value: {0}", _dateTimeValue);
67+
}
68+
69+
[Benchmark]
70+
public void MultipleValuesFormat()
71+
{
72+
_ = string.Format("Int: {0}, Double: {1}, Bool: {2}", _intValue, _doubleValue, _boolValue);
73+
}
74+
75+
[Benchmark]
76+
public void MixedFormat()
77+
{
78+
_ = string.Format("String: {0}, Int: {1}, DateTime: {2}", _stringValue, _intValue, _dateTimeValue);
79+
}
80+
81+
[Benchmark]
82+
public void ComplexFormat()
83+
{
84+
_ = string.Format("Values - Byte: {0}, Long: {1}, Double: {2}, Bool: {3}", _byteValue, _longValue, _doubleValue, _boolValue);
85+
}
86+
87+
[Benchmark]
88+
public void ReorderedFormat()
89+
{
90+
_ = string.Format("Values: {2}, {1}, {0}", _intValue, _doubleValue, _boolValue);
91+
}
92+
93+
[Benchmark]
94+
public void RepeatedFormat()
95+
{
96+
_ = string.Format("Value {0} repeated: {0}, {0}", _intValue);
97+
}
98+
}
99+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using nanoFramework.Benchmark;
6+
using nanoFramework.Benchmark.Attributes;
7+
8+
namespace nanoFramework.CoreLibrary.Benchmarks.ToString
9+
{
10+
[IterationCount(5)]
11+
public class ToStringInterpolation
12+
{
13+
private int _intValue;
14+
private long _longValue;
15+
private double _doubleValue;
16+
private bool _boolValue;
17+
private byte _byteValue;
18+
private DateTime _dateTimeValue;
19+
private string _stringValue;
20+
21+
[Setup]
22+
public void Setup()
23+
{
24+
_intValue = 12345;
25+
_longValue = 1234567890123456L;
26+
_doubleValue = 123.456;
27+
_boolValue = true;
28+
_byteValue = 255;
29+
_dateTimeValue = new DateTime(2024, 1, 15, 10, 30, 45);
30+
_stringValue = "test";
31+
}
32+
33+
[Benchmark]
34+
public void Int32Interpolation()
35+
{
36+
_ = $"Value: {_intValue}";
37+
}
38+
39+
[Benchmark]
40+
public void Int64Interpolation()
41+
{
42+
_ = $"Value: {_longValue}";
43+
}
44+
45+
[Benchmark]
46+
public void DoubleInterpolation()
47+
{
48+
_ = $"Value: {_doubleValue}";
49+
}
50+
51+
[Benchmark]
52+
public void BooleanInterpolation()
53+
{
54+
_ = $"Value: {_boolValue}";
55+
}
56+
57+
[Benchmark]
58+
public void ByteInterpolation()
59+
{
60+
_ = $"Value: {_byteValue}";
61+
}
62+
63+
[Benchmark]
64+
public void DateTimeInterpolation()
65+
{
66+
_ = $"Value: {_dateTimeValue}";
67+
}
68+
69+
[Benchmark]
70+
public void MultipleValuesInterpolation()
71+
{
72+
_ = $"Int: {_intValue}, Double: {_doubleValue}, Bool: {_boolValue}";
73+
}
74+
75+
[Benchmark]
76+
public void MixedInterpolation()
77+
{
78+
_ = $"String: {_stringValue}, Int: {_intValue}, DateTime: {_dateTimeValue}";
79+
}
80+
81+
[Benchmark]
82+
public void ComplexInterpolation()
83+
{
84+
_ = $"Values - Byte: {_byteValue}, Long: {_longValue}, Double: {_doubleValue}, Bool: {_boolValue}";
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)