using KGySoft.CoreLibraries;
using KGySoft.Diagnostics;
public static void Main()
new PerformanceTest<int> { TestName = "long.MaxValue", Iterations = 1_000_000, Repeat = 3, CpuAffinity = null }
.AddCase(() => GetStringLengthLog10(long.MaxValue), "Log10")
.AddCase(() => GetStringLengthBruteForce(long.MaxValue), "Brute force")
.DumpResults(Console.Out);
new RandomizedPerformanceTest<int> { TestName = "Random values", Iterations = 1_000_000, Repeat = 3, CpuAffinity = null }
.AddCase(rnd => GetStringLengthLog10(rnd.NextInt64()), "Log10")
.AddCase(rnd => GetStringLengthBruteForce(rnd.NextInt64()), "Brute force")
.DumpResults(Console.Out);
private static int GetStringLengthLog10(long value)
return (int)Math.Log10(value) + 1 + sign;
private static int GetStringLengthBruteForce(long value)
if (value < 10) return 1 + sign;
if (value < 100) return 2 + sign;
if (value < 1000) return 3 + sign;
if (value < 10000) return 4 + sign;
if (value < 100000) return 5 + sign;
if (value < 1000000) return 6 + sign;
if (value < 10000000) return 7 + sign;
if (value < 100000000) return 8 + sign;
if (value < 1000000000) return 9 + sign;
if (value < 10000000000) return 10 + sign;
if (value < 100000000000) return 11 + sign;
if (value < 1000000000000) return 12 + sign;
if (value < 10000000000000) return 13 + sign;
if (value < 100000000000000) return 14 + sign;
if (value < 1000000000000000) return 15 + sign;
if (value < 10000000000000000) return 16 + sign;
if (value < 100000000000000000) return 17 + sign;
if (value < 1000000000000000000) return 18 + sign;
public class RandomizedPerformanceTest<T> : PerformanceTestBase<Func<Random, T>, T>
protected override void OnBeforeCase() => random = new Random(0);
protected override T Invoke(Func<Random, T> del) => del.Invoke(random);