Method | Mean | Error | StdDev | Median | Gen 0 | Allocated |
------------------------------------- |-----------:|----------:|-----------:|-----------:|-------:|----------:|
Lyrcaxis_OriginalCode | 2,553.6 ns | 49.699 ns | 53.177 ns | 2,548.6 ns | 0.1526 | 891 B |
StringBuilder | 1,908.8 ns | 31.472 ns | 24.571 ns | 1,896.5 ns | 0.0687 | 417 B |
StringConcat | 1,990.0 ns | 38.910 ns | 36.396 ns | 1,980.9 ns | 0.1068 | 626 B |
NoPadLeft | 2,213.3 ns | 78.367 ns | 231.066 ns | 2,107.1 ns | 0.0992 | 578 B |
StringBuilder_NoPadLeft | 1,891.5 ns | 37.899 ns | 105.646 ns | 1,857.5 ns | 0.0553 | 321 B |
Math | 1,887.0 ns | 43.834 ns | 129.244 ns | 1,844.5 ns | 0.1163 | 674 B |
StringBuilder_Math | 1,577.2 ns | 31.641 ns | 40.016 ns | 1,571.8 ns | 0.0381 | 225 B |
Math_And_Prebuilt_Days | 448.1 ns | 14.374 ns | 41.931 ns | 438.2 ns | 0.0973 | 562 B |
StringBuilder_Math_And_Prebuilt_Days | 150.8 ns | 3.137 ns | 8.154 ns | 149.3 ns | 0.0179 | 104 B |
Last active
October 8, 2019 04:42
-
-
Save davidwengier/92ed08d3ab658920e52954ba8674e0a6 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; | |
using System.Text; | |
using BenchmarkDotNet.Attributes; | |
namespace BenchmarkFull | |
{ | |
// @Lyrcaxis on the C# discord asked for help improving the string interpolation code below, and someone dared suggest that string.Concat wasn't good | |
// and I couldn't let that rest! | |
[MemoryDiagnoser] | |
public class TimeFormatting | |
{ | |
// credit to @sc_holden for thinking of doing this | |
private readonly string[] PrebuiltDaysString = new string[] { | |
") " + DayOfWeek.Sunday+ " ", | |
") " + DayOfWeek.Monday + " ", | |
") " + DayOfWeek.Tuesday + " ", | |
") " + DayOfWeek.Wednesday+ " ", | |
") " + DayOfWeek.Thursday+ " ", | |
") " + DayOfWeek.Friday+ " ", | |
") " + DayOfWeek.Saturday+ " " | |
}; | |
private DayOfWeek Day = DateTime.Now.DayOfWeek; | |
private int Week = 12; // actually week number | |
private int Hour = DateTime.Now.Hour; | |
private int Minute = DateTime.Now.Minute; | |
private int Second = DateTime.Now.Second; | |
private StringBuilder builder = new StringBuilder(); | |
[Benchmark] | |
public string Lyrcaxis_OriginalCode() | |
{ | |
return $"({(int)Day + (Week * 7)}) {Day} {Hour:00}:{Minute:00}:{Second:00}"; | |
} | |
[Benchmark] | |
public string StringBuilder() | |
{ | |
builder.Clear(); | |
builder.Append("("); | |
builder.Append((int)Day + (Week * 7)); | |
builder.Append(") "); | |
builder.Append(Day); | |
builder.Append(" "); | |
builder.Append(Hour.ToString().PadLeft(2, '0')); | |
builder.Append(":"); | |
builder.Append(Minute.ToString().PadLeft(2, '0')); | |
builder.Append(":"); | |
builder.Append(Second.ToString().PadLeft(2, '0')); | |
return builder.ToString(); | |
} | |
[Benchmark] | |
public string StringConcat() | |
{ | |
return "(" + | |
((int)Day + (Week * 7)) + | |
") " + | |
Day + | |
" " + | |
Hour.ToString().PadLeft(2, '0') + | |
":" + | |
Minute.ToString().PadLeft(2, '0') + | |
":" + | |
Second.ToString().PadLeft(2, '0'); | |
} | |
[Benchmark] | |
public string NoPadLeft() | |
{ | |
return "(" + | |
((int)Day + (Week * 7)) + | |
") " + | |
Day + | |
" " + | |
(Hour < 10 ? "0" : "") + Hour + | |
":" + | |
(Minute < 10 ? "0" : "") + Minute + | |
":" + | |
(Second < 10 ? "0" : "") + Second; | |
} | |
[Benchmark] | |
public string StringBuilder_NoPadLeft() | |
{ | |
builder.Clear(); | |
builder.Append("("); | |
builder.Append((int)Day + (Week * 7)); | |
builder.Append(") "); | |
builder.Append(Day); | |
builder.Append(" "); | |
if (Hour < 10) | |
{ | |
builder.Append("0"); | |
} | |
builder.Append(Hour); | |
builder.Append(":"); | |
if (Minute < 10) | |
{ | |
builder.Append("0"); | |
} | |
builder.Append(Minute); | |
builder.Append(":"); | |
if (Second < 10) | |
{ | |
builder.Append("0"); | |
} | |
builder.Append(Second); | |
return builder.ToString(); | |
} | |
[Benchmark] | |
public string Math() | |
{ | |
return "(" + | |
((int)Day + (Week * 7)) + | |
") " + | |
Day + | |
" " + | |
(char)((Hour / 10) + 48) + | |
(char)((Hour % 10) + 48) + | |
":" + | |
(char)((Minute / 10) + 48) + | |
(char)((Minute % 10) + 48) + | |
":" + | |
(char)((Second / 10) + 48) + | |
(char)((Second % 10) + 48); | |
} | |
[Benchmark] | |
public string StringBuilder_Math() | |
{ | |
builder.Clear(); | |
builder.Append("("); | |
builder.Append((int)Day + (Week * 7)); | |
builder.Append(") "); | |
builder.Append(Day); | |
builder.Append(" "); | |
builder.Append((char)((Hour / 10) + 48)); | |
builder.Append((char)((Hour % 10) + 48)); | |
builder.Append(":"); | |
builder.Append((char)((Minute / 10) + 48)); | |
builder.Append((char)((Minute % 10) + 48)); | |
builder.Append(":"); | |
builder.Append((char)((Second / 10) + 48)); | |
builder.Append((char)((Second % 10) + 48)); | |
return builder.ToString(); | |
} | |
[Benchmark] | |
public string Math_And_Prebuilt_Days() | |
{ | |
var dayNumber = (int)Day; | |
return "(" + | |
(dayNumber + (Week * 7)) + | |
") " + | |
PrebuiltDaysString[dayNumber] + | |
" " + | |
(char)((Hour / 10) + 48) + | |
(char)((Hour % 10) + 48) + | |
":" + | |
(char)((Minute / 10) + 48) + | |
(char)((Minute % 10) + 48) + | |
":" + | |
(char)((Second / 10) + 48) + | |
(char)((Second % 10) + 48); | |
} | |
[Benchmark] | |
public string StringBuilder_Math_And_Prebuilt_Days() | |
{ | |
var dayNumber = (int)Day; | |
builder.Clear(); | |
builder.Append("("); | |
builder.Append(dayNumber + (Week * 7)); | |
builder.Append(PrebuiltDaysString[dayNumber]); | |
builder.Append((char)((Hour / 10) + 48)); | |
builder.Append((char)((Hour % 10) + 48)); | |
builder.Append(":"); | |
builder.Append((char)((Minute / 10) + 48)); | |
builder.Append((char)((Minute % 10) + 48)); | |
builder.Append(":"); | |
builder.Append((char)((Second / 10) + 48)); | |
builder.Append((char)((Second % 10) + 48)); | |
return builder.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment