Created
July 29, 2020 12:05
-
-
Save nyctef/602044d76b7528ced488ebc3e5682a4c 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.Security.Cryptography; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
namespace MyBenchmarks | |
{ | |
public class IfVsSwitch | |
{ | |
private Random random = new Random(42); | |
public IfVsSwitch() | |
{ | |
} | |
[Benchmark] | |
public string Ifs() { | |
var day = random.Next(7); | |
if (day == 0) { return "Monday"; } | |
else if (day == 1) { return "Tuesday"; } | |
else if (day == 2) { return "Wednesday"; } | |
else if (day == 3) { return "Thursday"; } | |
else if (day == 4) { return "Friday"; } | |
else if (day == 5) { return "Saturday"; } | |
else if (day == 6) { return "Sunday"; } | |
else { return "Unknown"; } | |
} | |
[Benchmark] | |
public string Switch() { | |
switch (random.Next(7)) { | |
case 0: return "Monday"; | |
case 1: return "Tuesday"; | |
case 2: return "Wednesday"; | |
case 3: return "Thursday"; | |
case 4: return "Friday"; | |
case 5: return "Saturday"; | |
case 6: return "Sunday"; | |
default: return "Unknown"; | |
} | |
} | |
[Benchmark] | |
public int JustRandom() { | |
return random.Next(7); | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<IfVsSwitch>(); | |
} | |
} | |
} | |
/* | |
Output from `dotnet run --configuration Release`: | |
// Benchmark Process 17604 has exited with code 0 | |
Mean = 12.138 ns, StdErr = 0.013 ns (0.11%), N = 14, StdDev = 0.048 ns | |
Min = 12.066 ns, Q1 = 12.108 ns, Median = 12.123 ns, Q3 = 12.173 ns, Max = 12.212 ns | |
IQR = 0.065 ns, LowerFence = 12.010 ns, UpperFence = 12.270 ns | |
ConfidenceInterval = [12.084 ns; 12.192 ns] (CI 99.9%), Margin = 0.054 ns (0.44% of Mean) | |
Skewness = 0.26, Kurtosis = 1.64, MValue = 2 | |
// ***** BenchmarkRunner: Finish ***** | |
// * Export * | |
BenchmarkDotNet.Artifacts\results\MyBenchmarks.IfVsSwitch-report.csv | |
BenchmarkDotNet.Artifacts\results\MyBenchmarks.IfVsSwitch-report-github.md | |
BenchmarkDotNet.Artifacts\results\MyBenchmarks.IfVsSwitch-report.html | |
// * Detailed results * | |
IfVsSwitch.Ifs: DefaultJob | |
Runtime = .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT; GC = Concurrent Workstation | |
Mean = 20.247 ns, StdErr = 0.020 ns (0.10%), N = 14, StdDev = 0.076 ns | |
Min = 20.134 ns, Q1 = 20.197 ns, Median = 20.240 ns, Q3 = 20.298 ns, Max = 20.385 ns | |
IQR = 0.100 ns, LowerFence = 20.047 ns, UpperFence = 20.448 ns | |
ConfidenceInterval = [20.161 ns; 20.333 ns] (CI 99.9%), Margin = 0.086 ns (0.43% of Mean) | |
Skewness = 0.35, Kurtosis = 1.95, MValue = 2 | |
-------------------- Histogram -------------------- | |
[20.093 ns ; 20.426 ns) | @@@@@@@@@@@@@@ | |
--------------------------------------------------- | |
IfVsSwitch.Switch: DefaultJob | |
Runtime = .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT; GC = Concurrent Workstation | |
Mean = 21.547 ns, StdErr = 0.052 ns (0.24%), N = 15, StdDev = 0.201 ns | |
Min = 21.302 ns, Q1 = 21.408 ns, Median = 21.471 ns, Q3 = 21.684 ns, Max = 22.001 ns | |
IQR = 0.276 ns, LowerFence = 20.994 ns, UpperFence = 22.098 ns | |
ConfidenceInterval = [21.332 ns; 21.761 ns] (CI 99.9%), Margin = 0.214 ns (0.99% of Mean) | |
Skewness = 0.83, Kurtosis = 2.42, MValue = 2 | |
-------------------- Histogram -------------------- | |
[21.195 ns ; 22.107 ns) | @@@@@@@@@@@@@@@ | |
--------------------------------------------------- | |
IfVsSwitch.JustRandom: DefaultJob | |
Runtime = .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT; GC = Concurrent Workstation | |
Mean = 12.138 ns, StdErr = 0.013 ns (0.11%), N = 14, StdDev = 0.048 ns | |
Min = 12.066 ns, Q1 = 12.108 ns, Median = 12.123 ns, Q3 = 12.173 ns, Max = 12.212 ns | |
IQR = 0.065 ns, LowerFence = 12.010 ns, UpperFence = 12.270 ns | |
ConfidenceInterval = [12.084 ns; 12.192 ns] (CI 99.9%), Margin = 0.054 ns (0.44% of Mean) | |
Skewness = 0.26, Kurtosis = 1.64, MValue = 2 | |
-------------------- Histogram -------------------- | |
[12.040 ns ; 12.239 ns) | @@@@@@@@@@@@@@ | |
--------------------------------------------------- | |
// * Summary * | |
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.388 (2004/?/20H1) | |
Intel Core i7-4770K CPU 3.50GHz (Haswell), 1 CPU, 8 logical and 4 physical cores | |
.NET Core SDK=3.1.302 | |
[Host] : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT | |
DefaultJob : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT | |
| Method | Mean | Error | StdDev | | |
|----------- |---------:|---------:|---------:| | |
| Ifs | 20.25 ns | 0.086 ns | 0.076 ns | | |
| Switch | 21.55 ns | 0.214 ns | 0.201 ns | | |
| JustRandom | 12.14 ns | 0.054 ns | 0.048 ns | | |
// * Hints * | |
Outliers | |
IfVsSwitch.Ifs: Default -> 1 outlier was removed (24.35 ns) | |
IfVsSwitch.JustRandom: Default -> 1 outlier was removed (13.92 ns) | |
// * Legends * | |
Mean : Arithmetic mean of all measurements | |
Error : Half of 99.9% confidence interval | |
StdDev : Standard deviation of all measurements | |
1 ns : 1 Nanosecond (0.000000001 sec) | |
// ***** BenchmarkRunner: End ***** | |
// ** Remained 0 benchmark(s) to run ** | |
Run time: 00:01:13 (73.12 sec), executed benchmarks: 3 | |
Global total time: 00:01:17 (77.39 sec), executed benchmarks: 3 | |
// * Artifacts cleanup * | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment