Код:
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
var summary = BenchmarkRunner.Run<DetectLuckyTicket>();
[MemoryDiagnoser]
public class DetectLuckyTicket
{
private readonly int ticketNumber;
public DetectLuckyTicket()
{
ticketNumber = Random.Shared.Next(1, 1000000);
}
[Benchmark]
public bool IsLuckyFast()
{
var leftPart = ticketNumber / 1000;
var leftPartSum = (leftPart / 100) + (leftPart / 10 % 10) + (leftPart % 100);
var rightPart = ticketNumber % 1000;
var rightPartSum = (rightPart / 100) + (rightPart / 10 % 10) + (rightPart % 100);
return leftPartSum == rightPartSum;
}
[Benchmark]
public bool IsLuckySlow()
{
var kstr = ticketNumber.ToString("D6").ToCharArray();
var leftPartSum = (kstr[0] - '0' + kstr[1] - '0' + kstr[2] - '0');
var rightPartSum = (kstr[3] - '0' + kstr[4] - '0' + kstr[5] - '0');
return leftPartSum == rightPartSum;
}
}
Результат:
// * Detailed results *
DetectLuckyTicket.IsLuckyFast: DefaultJob
Runtime = .NET 7.0.0 (7.0.22.15202), X64 RyuJIT; GC = Concurrent Workstation
Mean = 6.417 ns, StdErr = 0.039 ns (0.60%), N = 15, StdDev = 0.150 ns
Min = 6.113 ns, Q1 = 6.318 ns, Median = 6.408 ns, Q3 = 6.505 ns, Max = 6.671 ns
IQR = 0.188 ns, LowerFence = 6.036 ns, UpperFence = 6.787 ns
ConfidenceInterval = [6.257 ns; 6.577 ns] (CI 99.9%), Margin = 0.160 ns (2.49% of Mean)
Skewness = -0.06, Kurtosis = 2.24, MValue = 2
-------------------- Histogram --------------------
[6.034 ns ; 6.193 ns) | @
[6.193 ns ; 6.427 ns) | @@@@@@@@
[6.427 ns ; 6.606 ns) | @@@@
[6.606 ns ; 6.751 ns) | @@
---------------------------------------------------
DetectLuckyTicket.IsLuckySlow: DefaultJob
Runtime = .NET 7.0.0 (7.0.22.15202), X64 RyuJIT; GC = Concurrent Workstation
Mean = 32.280 ns, StdErr = 0.250 ns (0.77%), N = 100, StdDev = 2.496 ns
Min = 28.418 ns, Q1 = 30.360 ns, Median = 31.542 ns, Q3 = 33.921 ns, Max = 37.623 ns
IQR = 3.561 ns, LowerFence = 25.018 ns, UpperFence = 39.264 ns
ConfidenceInterval = [31.434 ns; 33.127 ns] (CI 99.9%), Margin = 0.847 ns (2.62% of Mean)
Skewness = 0.63, Kurtosis = 2.09, MValue = 2.91
-------------------- Histogram --------------------
[28.207 ns ; 29.529 ns) | @@@@@@@
[29.529 ns ; 30.941 ns) | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[30.941 ns ; 32.596 ns) | @@@@@@@@@@@@@@@@@@@@@@@
[32.596 ns ; 33.910 ns) | @@@@@@@@@@
[33.910 ns ; 35.321 ns) | @@@
[35.321 ns ; 36.733 ns) | @@@@@@@@@@@@@@@@@@@
[36.733 ns ; 38.329 ns) | @@@
---------------------------------------------------
// * Summary *
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.25131
AMD Ryzen 5 4600H with Radeon Graphics, 1 CPU, 12 logical and 6 physical cores
.NET SDK=7.0.100-preview.2.22153.17
[Host] : .NET 7.0.0 (7.0.22.15202), X64 RyuJIT
DefaultJob : .NET 7.0.0 (7.0.22.15202), X64 RyuJIT
| Method | Mean | Error | StdDev | Median | Gen 0 | Allocated |
|------------ |----------:|----------:|----------:|----------:|-------:|----------:|
| IsLuckyFast | 6.417 ns | 0.1599 ns | 0.1496 ns | 6.408 ns | - | - |
| IsLuckySlow | 32.280 ns | 0.8467 ns | 2.4964 ns | 31.542 ns | 0.0382 | 80 B |
// * Warnings *
MultimodalDistribution
DetectLuckyTicket.IsLuckySlow: Default -> It seems that the distribution can have several modes (mValue = 2.91)
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Median : Value separating the higher half of all measurements (50th percentile)
Gen 0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)