diff --git a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/PatternMatchingVsComplexIfElseBenchmarkTests.cs b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/PatternMatchingVsComplexIfElseBenchmarkTests.cs index c796183389..f0173593ad 100644 --- a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/PatternMatchingVsComplexIfElseBenchmarkTests.cs +++ b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/PatternMatchingVsComplexIfElseBenchmarkTests.cs @@ -3,56 +3,58 @@ namespace SwitchVsIfElsePerformanceTests.BenchmarkTests; +[InProcess] public class PatternMatchingVsComplexIfElseBenchmarkTests { - private object[] _shapes = { + private readonly object[] _shapes = + [ new Circle { Radius = 12 }, new Circle { Radius = 4 }, new Rectangle { Width = 4, Height = 4 }, new Rectangle { Width = 4, Height = 10 }, new Line { Length = 10 }, - }; - - [Params(1000)] - public int N; - + ]; + [Benchmark] - public void IfElseWithComplexConditions() + public int IfElseWithComplexConditions() { - string shapeName; - + var totalLength = 0; + foreach (object shape in _shapes) { - shapeName = ClassifyShapeWithIfElse(shape); + totalLength += ClassifyShapeWithIfElse(shape).Length; } + + return totalLength; } [Benchmark] - public void SwitchExpressionWithPatternMatching() + public int SwitchExpressionWithPatternMatching() { - string shapeName; - + var totalLength = 0; + foreach (object shape in _shapes) { - shapeName = ClassifyShapeWithPatternMathing(shape); + totalLength += ClassifyShapeWithPatternMatching(shape).Length; } + + return totalLength; } - private string ClassifyShapeWithIfElse(object shape) + private static string ClassifyShapeWithIfElse(object shape) { - if (shape is Circle) + if (shape is Circle circle) { - if (((Circle)shape).Radius > 10) + if (circle.Radius > 10) { return "Large Circle"; } return "Circle"; } - if (shape is Rectangle) + + if (shape is Rectangle rectangle) { - var rectangle = shape as Rectangle; - if (rectangle.Height == rectangle.Width) { return "Square"; @@ -63,9 +65,8 @@ private string ClassifyShapeWithIfElse(object shape) return "Unknown Shape"; } - - - private string ClassifyShapeWithPatternMathing(object shape) => + + private static string ClassifyShapeWithPatternMatching(object shape) => shape switch { Circle { Radius: > 10 } => "Large Circle", @@ -74,4 +75,4 @@ private string ClassifyShapeWithPatternMathing(object shape) => Rectangle => "Rectangle", _ => "Unknown Shape" }; -} \ No newline at end of file +} diff --git a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/SwitchVsIfElseBenchmarkTests.cs b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/SwitchVsIfElseBenchmarkTests.cs index 9d098c93ee..8351e7b07d 100644 --- a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/SwitchVsIfElseBenchmarkTests.cs +++ b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/BenchmarkTests/SwitchVsIfElseBenchmarkTests.cs @@ -2,16 +2,17 @@ namespace SwitchVsIfElsePerformanceTests.BenchmarkTests; +[InProcess] public class SwitchVsIfElseBenchmarkTests { - private readonly int _iterations; - private readonly int[] _inputData; - - public SwitchVsIfElseBenchmarkTests() + private readonly int _iterations = 1000; + private int[] _inputData = []; + + [GlobalSetup] + public void Setup() { - _iterations = 1000; _inputData = new int[_iterations]; - + for (int i = 0; i < _iterations; i++) { _inputData[i] = i % 10; @@ -19,10 +20,11 @@ public SwitchVsIfElseBenchmarkTests() } [Benchmark] - public void IfElseWithOneCondition() + public int IfElseWithOneCondition() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { if (_inputData[i] == 1) @@ -33,33 +35,43 @@ public void IfElseWithOneCondition() { result = -1; } + + sum += result; } + + return sum; } [Benchmark] - public void SwitchWithOneCase() + public int SwitchWithOneCase() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { switch (_inputData[i]) { - case 1: + case 1: result = 1; break; - default: + default: result = -1; break; } + + sum += result; } + + return sum; } - + [Benchmark] - public void SwitchExpressionWithOneCase() + public int SwitchExpressionWithOneCase() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { result = _inputData[i] switch @@ -67,14 +79,19 @@ public void SwitchExpressionWithOneCase() 1 => 1, _ => -1 }; + + sum += result; } + + return sum; } - + [Benchmark] - public void IfElseWithTwoConditions() + public int IfElseWithTwoConditions() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { if (_inputData[i] == 1) @@ -89,36 +106,46 @@ public void IfElseWithTwoConditions() { result = -1; } + + sum += result; } + + return sum; } [Benchmark] - public void SwitchWithTwoCases() + public int SwitchWithTwoCases() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { switch (_inputData[i]) { - case 1: + case 1: result = 1; break; - case 2: + case 2: result = 2; break; - default: + default: result = -1; break; } + + sum += result; } + + return sum; } - + [Benchmark] - public void SwitchExpressionWithTwoCases() + public int SwitchExpressionWithTwoCases() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { result = _inputData[i] switch @@ -127,14 +154,19 @@ public void SwitchExpressionWithTwoCases() 2 => 2, _ => -1 }; + + sum += result; } + + return sum; } - + [Benchmark] - public void IfElseWithFiveConditions() + public int IfElseWithFiveConditions() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { if (_inputData[i] == 1) @@ -161,45 +193,55 @@ public void IfElseWithFiveConditions() { result = -1; } + + sum += result; } + + return sum; } [Benchmark] - public void SwitchWithFiveCases() + public int SwitchWithFiveCases() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { switch (_inputData[i]) { - case 1: + case 1: result = 1; break; - case 2: + case 2: result = 2; break; - case 3: + case 3: result = 3; break; - case 4: + case 4: result = 4; break; - case 5: + case 5: result = 5; break; - default: + default: result = -1; break; } + + sum += result; } + + return sum; } - + [Benchmark] - public void SwitchExpressionWithFiveCases() + public int SwitchExpressionWithFiveCases() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { result = _inputData[i] switch @@ -211,14 +253,19 @@ public void SwitchExpressionWithFiveCases() 5 => 5, _ => -1 }; + + sum += result; } + + return sum; } - + [Benchmark] - public void IfElseWithTenConditions() + public int IfElseWithTenConditions() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { if (_inputData[i] == 1) @@ -265,60 +312,70 @@ public void IfElseWithTenConditions() { result = -1; } + + sum += result; } + + return sum; } [Benchmark] - public void SwitchWithTenCases() + public int SwitchWithTenCases() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { switch (_inputData[i]) { - case 1: + case 1: result = 1; break; - case 2: + case 2: result = 2; break; - case 3: + case 3: result = 3; break; - case 4: + case 4: result = 4; break; - case 5: + case 5: result = 5; break; - case 6: + case 6: result = 6; break; - case 7: + case 7: result = 7; break; - case 8: + case 8: result = 8; break; - case 9: + case 9: result = 9; break; - case 0: + case 0: result = 0; break; - default: + default: result = -1; break; } + + sum += result; } + + return sum; } - + [Benchmark] - public void SwitchExpressionWithTenCases() + public int SwitchExpressionWithTenCases() { int result; - + var sum = 0; + for (int i = 0; i < _iterations; i++) { result = _inputData[i] switch @@ -335,6 +392,10 @@ public void SwitchExpressionWithTenCases() 0 => 0, _ => -1 }; + + sum += result; } + + return sum; } -} \ No newline at end of file +} diff --git a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Examples/ControlFlowExamples.cs b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Examples/ControlFlowExamples.cs new file mode 100644 index 0000000000..6dd1da0350 --- /dev/null +++ b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Examples/ControlFlowExamples.cs @@ -0,0 +1,40 @@ +namespace SwitchVsIfElsePerformanceTests.Examples; + +public static class ControlFlowExamples +{ + public static string DescribeWithIfElse(int variable) + { + if (variable == 1) + { + return "First case"; + } + else if (variable == 2) + { + return "Second case"; + } + else + { + return "Default case"; + } + } + + public static string DescribeWithSwitch(int variable) + { + switch (variable) + { + case 1: + return "First case"; + case 2: + return "Second case"; + default: + return "Default case"; + } + } + + public static string DescribeWithSwitchExpression(int variable) => variable switch + { + 1 => "First case", + 2 => "Second case", + _ => "Default case", + }; +} diff --git a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Examples/SwitchLoweringExamples.cs b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Examples/SwitchLoweringExamples.cs new file mode 100644 index 0000000000..448916dad2 --- /dev/null +++ b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Examples/SwitchLoweringExamples.cs @@ -0,0 +1,40 @@ +namespace SwitchVsIfElsePerformanceTests.Examples; + +public static class SwitchLoweringExamples +{ + public static string AnimalSounds(string animal) + { + switch (animal) + { + case "dog": + return "Woof"; + case "cat": + return "Meow"; + default: + return ""; + } + } + + public static string AnimalSoundsWithMoreCases(string animal) + { + switch (animal) + { + case "dog": + return "Woof"; + case "cat": + return "Meow"; + case "horse": + return "Neigh"; + case "bird": + return "Chirp"; + case "fish": + return ""; + case "mouse": + return "Peep"; + case "oyster": + return "..."; + default: + return ""; + } + } +} diff --git a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Program.cs b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Program.cs index d90f6fd2ff..af5ba4f059 100644 --- a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Program.cs +++ b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/Program.cs @@ -1,5 +1,3 @@ -// See https://aka.ms/new-console-template for more information - -using BenchmarkDotNet.Running; +using BenchmarkDotNet.Running; BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); \ No newline at end of file diff --git a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/SwitchVsIfElsePerformanceTests.csproj b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/SwitchVsIfElsePerformanceTests.csproj index 9b44239a6a..1a65bee991 100644 --- a/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/SwitchVsIfElsePerformanceTests.csproj +++ b/dotnet-performance/SwitchVsIfElse/SwitchVsIfElsePerformanceTests/SwitchVsIfElsePerformanceTests.csproj @@ -2,13 +2,13 @@ Exe - net8.0 + net11.0 enable enable - +