CSharp · Console
using System;
using KGySoft.CoreLibraries;
using KGySoft.Diagnostics;
public class Program
{
public static void Main()
{
ConsoleColor value = ConsoleColor.Blue;
DateTime timeStamp = DateTime.UtcNow;
new PerformanceTest<string>
{
TestTime = 100, // Only 100ms warmup to prevent too high pressure on .NET Fiddle - comment this out when testing at home
Iterations = 1_000_000, // comment out also this for time based testing instead
Repeat = 3,
}
.AddCase(() => ToStringBySwitch(value), "'switched' ToString")
.AddCase(() => value.ToString(), "Enum.ToString")
//.AddCase(() => Enum<ConsoleColor>.ToString(value), "KGySoft's ToString")
// comment out the cases above to compare only the 'real-life' cases (just a simple interpolation without doing anything with the log entry)
.AddCase(() => $"[DEBUG][{timeStamp:O}]The value of the color was '{ToStringBySwitch(value)}'", "Interpolation with 'switched' ToString")
.AddCase(() => $"[DEBUG][{timeStamp:O}]The value of the color was '{value}'", "Interpolation with Enum.ToString")
//.AddCase(() => $"[DEBUG][{timeStamp:O}]The value of the color was '{Enum<ConsoleColor>.ToString(value)}'", "Interpolation with KGySoft's ToString")
.DoTest()
.DumpResults(Console.Out);
}
// remark: the performance might decrease with larger enums
private static string ToStringBySwitch(ConsoleColor color) => color switch
{
ConsoleColor.Black => nameof(ConsoleColor.Black),
ConsoleColor.DarkBlue => nameof(ConsoleColor.DarkBlue),
ConsoleColor.DarkGreen => nameof(ConsoleColor.DarkGreen),
ConsoleColor.DarkCyan => nameof(ConsoleColor.DarkCyan),
ConsoleColor.DarkRed => nameof(ConsoleColor.DarkRed),
ConsoleColor.DarkMagenta => nameof(ConsoleColor.DarkMagenta),
ConsoleColor.DarkYellow => nameof(ConsoleColor.DarkYellow),
ConsoleColor.Gray => nameof(ConsoleColor.Gray),
ConsoleColor.DarkGray => nameof(ConsoleColor.DarkGray),
ConsoleColor.Blue => nameof(ConsoleColor.Blue),
ConsoleColor.Green => nameof(ConsoleColor.Green),
ConsoleColor.Cyan => nameof(ConsoleColor.Cyan),
ConsoleColor.Red => nameof(ConsoleColor.Red),
ConsoleColor.Magenta => nameof(ConsoleColor.Magenta),
ConsoleColor.Yellow => nameof(ConsoleColor.Yellow),
ConsoleColor.White => nameof(ConsoleColor.White),
_ => throw new ArgumentOutOfRangeException(nameof(color))
};
}