Last active
January 9, 2025 13:30
-
-
Save jasdefer/45927268961ccf22e24bd0d92517731f to your computer and use it in GitHub Desktop.
A benchmark to test the performance of integer vs string switch statements.
This file contains hidden or 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 BenchmarkDotNet.Attributes; | |
| namespace AgentMobilityNet.Benchmark; | |
| [MemoryDiagnoser] | |
| public class SwitchBenchmarks | |
| { | |
| [Params(1,100, 10000)] | |
| public int Size { get; set; } | |
| private static readonly string[] _strings = new string[10000]; | |
| private static readonly int[] _ints = new int[10000]; | |
| [GlobalSetup] | |
| public void Setup() | |
| { | |
| for (int i = 0; i < 10000; i++) | |
| { | |
| int number = i % 3; | |
| _strings[i] = number.ToString(); | |
| _ints[i] = number; | |
| } | |
| } | |
| [Benchmark] | |
| public int Int() | |
| { | |
| int sum = 0; | |
| for (int i = 0; i < Size; i++) | |
| { | |
| int number = _ints[i]; | |
| sum += number switch | |
| { | |
| 0 => 0, | |
| 1 => 1, | |
| 2 => 2, | |
| _ => throw new Exception() | |
| }; | |
| } | |
| return sum; | |
| } | |
| [Benchmark] | |
| public int String() | |
| { | |
| int sum = 0; | |
| for (int i = 0; i < Size; i++) | |
| { | |
| sum += _strings[i] switch | |
| { | |
| "0" => 0, | |
| "1" => 1, | |
| "2" => 2, | |
| _ => throw new Exception() | |
| }; | |
| } | |
| return sum; | |
| } | |
| [Benchmark] | |
| public int IntIf() | |
| { | |
| int sum = 0; | |
| for (int i = 0; i < Size; i++) | |
| { | |
| int number = _ints[i]; | |
| if (!(number == 0)) | |
| { | |
| if (!(number == 1)) | |
| { | |
| if (!(number == 2)) | |
| { | |
| throw new Exception(); | |
| } | |
| sum += 2; | |
| } | |
| else | |
| { | |
| sum += 1; | |
| } | |
| } | |
| } | |
| return sum; | |
| } | |
| } | |
| | Method | Size | Mean | Error | StdDev | Allocated | | |
| |------- |------ |---------------:|------------:|------------:|----------:| | |
| | Int | 1 | 1.1818 ns | 0.0315 ns | 0.0263 ns | - | | |
| | IntIf | 1 | 0.7574 ns | 0.0040 ns | 0.0033 ns | - | | |
| | String | 1 | 1.3795 ns | 0.0092 ns | 0.0086 ns | - | | |
| | Record | 1 | 1.4888 ns | 0.0093 ns | 0.0087 ns | - | | |
| | Int | 100 | 139.4602 ns | 0.1135 ns | 0.1062 ns | - | | |
| | IntIf | 100 | 73.4985 ns | 0.4778 ns | 0.4469 ns | - | | |
| | String | 100 | 118.0915 ns | 0.6409 ns | 0.5995 ns | - | | |
| | Record | 100 | 324.7322 ns | 1.3668 ns | 1.2785 ns | - | | |
| | Int | 10000 | 15,932.6126 ns | 16.9945 ns | 15.8967 ns | - | | |
| | IntIf | 10000 | 6,558.3073 ns | 42.6984 ns | 35.6551 ns | - | | |
| | String | 10000 | 11,309.7840 ns | 86.6066 ns | 76.7745 ns | - | | |
| | Record | 10000 | 26,570.9096 ns | 279.0804 ns | 217.8876 ns | - | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment