Created
October 16, 2023 17:25
-
-
Save davepcallan/b876848a1b7206e4fbc724aa0d828b3a to your computer and use it in GitHub Desktop.
Benchmarks using the params keyword in C#
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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Columns; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Reports; | |
namespace BenchmarkDotNet.Samples | |
{ | |
[Config(typeof(Config))] | |
[SimpleJob(RuntimeMoniker.Net80)] | |
[MemoryDiagnoser] | |
[ReturnValueValidator(failOnError:true)] | |
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)] | |
public class ParamsBenchmark | |
{ | |
[Benchmark(Baseline = true)] | |
public string MethodWith3Params() => StandardMethod3("param1", "param2", "param3"); | |
[Benchmark] | |
public string MethodWithParams3() => ParamsMethod("param1", "param2", "param3"); | |
[Benchmark] | |
public string MethodWithArray3() => ArrayMethod(new string[] { "param1", "param2", "param3" }); | |
public string StandardMethod3(string p1, string p2, string p3) => p1 + p2 + p3; | |
public string ParamsMethod(params string[] args) | |
{ | |
var result = ""; | |
foreach (var arg in args) | |
{ | |
result += arg; | |
} | |
return result; | |
} | |
public string ArrayMethod(string[] args) | |
{ | |
var result = ""; | |
foreach (var arg in args) | |
{ | |
result += arg; | |
} | |
return result; | |
} | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
SummaryStyle = | |
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment