Skip to content

Instantly share code, notes, and snippets.

@abatishchev
Last active June 29, 2022 04:06
Show Gist options
  • Save abatishchev/6a6660335ae0491f59cb7ea1db7c79da to your computer and use it in GitHub Desktop.
Save abatishchev/6a6660335ae0491f59cb7ea1db7c79da to your computer and use it in GitHub Desktop.
SwitchCaseIfElseBenchmark
using System.Runtime.CompilerServices;
using System.Threading;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
namespace SwitchCaseIfElseBenchmark
{
public static class Program
{
public static void Main()
{
var summary = BenchmarkRunner.Run(typeof(GraphTenantBenchmark).Assembly);
}
}
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net462, baseline: true)]
[SimpleJob(RuntimeMoniker.Net60)]
public class GraphTenantBenchmark
{
[Benchmark(Baseline = true)]
[Arguments("one")]
[Arguments("two")]
[Arguments("three")]
[Arguments("four")]
[Arguments("five")]
public int IfElse(string input)
{
if (EqualsAndWait(input, "one"))
{
return 1;
}
if (EqualsAndWait(input, "two"))
{
return 2;
}
if (EqualsAndWait(input, "three"))
{
return 3;
}
if (EqualsAndWait(input, "four"))
{
return 4;
}
if (EqualsAndWait(input, "five"))
{
return 5;
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool EqualsAndWait(string input, string value)
{
Thread.Sleep(100);
return input == value;
}
[Benchmark]
[Arguments("one")]
[Arguments("two")]
[Arguments("three")]
[Arguments("four")]
[Arguments("five")]
public int SwitchCase(string input)
{
switch (input)
{
case "one":
{
Thread.Sleep(100);
return 1;
}
case "two":
{
Thread.Sleep(100);
return 2;
}
case "three":
{
Thread.Sleep(100);
return 3;
}
case "four":
{
Thread.Sleep(100);
return 4;
}
case "five":
{
Thread.Sleep(100);
return 5;
}
default:
{
Thread.Sleep(100);
return -1;
}
}
}
}
}
| Method | Job | Runtime | input | Mean | Error | StdDev | Ratio | RatioSD | Allocated |
|----------- |--------------------- |--------------------- |------ |---------:|--------:|--------:|------:|--------:|----------:|
| IfElse | .NET 6.0 | .NET 6.0 | five | 546.1 ms | 3.44 ms | 3.05 ms | 1.00 | 0.01 | 4,024 B |
| SwitchCase | .NET 6.0 | .NET 6.0 | five | 109.2 ms | 0.77 ms | 0.72 ms | 0.20 | 0.00 | 728 B |
| IfElse | .NET Framework 4.6.2 | .NET Framework 4.6.2 | five | 545.4 ms | 4.86 ms | 4.31 ms | 1.00 | 0.00 | - |
| SwitchCase | .NET Framework 4.6.2 | .NET Framework 4.6.2 | five | 108.7 ms | 0.74 ms | 0.69 ms | 0.20 | 0.00 | - |
| | | | | | | | | | |
| IfElse | .NET 6.0 | .NET 6.0 | four | 434.6 ms | 2.81 ms | 2.63 ms | 1.00 | 0.02 | 480 B |
| SwitchCase | .NET 6.0 | .NET 6.0 | four | 108.7 ms | 0.96 ms | 0.90 ms | 0.25 | 0.00 | 728 B |
| IfElse | .NET Framework 4.6.2 | .NET Framework 4.6.2 | four | 435.4 ms | 6.10 ms | 5.71 ms | 1.00 | 0.00 | - |
| SwitchCase | .NET Framework 4.6.2 | .NET Framework 4.6.2 | four | 108.9 ms | 0.81 ms | 0.76 ms | 0.25 | 0.00 | - |
| | | | | | | | | | |
| IfElse | .NET 6.0 | .NET 6.0 | one | 108.8 ms | 0.71 ms | 0.66 ms | 1.00 | 0.01 | 733 B |
| SwitchCase | .NET 6.0 | .NET 6.0 | one | 108.4 ms | 1.21 ms | 1.13 ms | 0.99 | 0.01 | 728 B |
| IfElse | .NET Framework 4.6.2 | .NET Framework 4.6.2 | one | 109.3 ms | 1.02 ms | 0.95 ms | 1.00 | 0.00 | - |
| SwitchCase | .NET Framework 4.6.2 | .NET Framework 4.6.2 | one | 108.7 ms | 0.68 ms | 0.61 ms | 0.99 | 0.01 | - |
| | | | | | | | | | |
| IfElse | .NET 6.0 | .NET 6.0 | three | 327.2 ms | 4.13 ms | 3.86 ms | 1.00 | 0.01 | 1,832 B |
| SwitchCase | .NET 6.0 | .NET 6.0 | three | 108.4 ms | 0.60 ms | 0.50 ms | 0.33 | 0.00 | 728 B |
| IfElse | .NET Framework 4.6.2 | .NET Framework 4.6.2 | three | 326.8 ms | 2.05 ms | 1.81 ms | 1.00 | 0.00 | - |
| SwitchCase | .NET Framework 4.6.2 | .NET Framework 4.6.2 | three | 109.5 ms | 1.04 ms | 0.97 ms | 0.34 | 0.00 | - |
| | | | | | | | | | |
| IfElse | .NET 6.0 | .NET 6.0 | two | 218.0 ms | 1.55 ms | 1.37 ms | 1.00 | 0.01 | 160 B |
| SwitchCase | .NET 6.0 | .NET 6.0 | two | 108.9 ms | 0.94 ms | 0.88 ms | 0.50 | 0.01 | 728 B |
| IfElse | .NET Framework 4.6.2 | .NET Framework 4.6.2 | two | 217.8 ms | 1.42 ms | 1.26 ms | 1.00 | 0.00 | - |
| SwitchCase | .NET Framework 4.6.2 | .NET Framework 4.6.2 | two | 108.7 ms | 0.83 ms | 0.78 ms | 0.50 | 0.00 | - |
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net462</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment