- C# 7 and .NET Core 2.0 High Performance
- Ovais Mehboob Ahmed Khan
- 115字
- 2021-08-27 18:47:15
Adding configurations
Benchmark configuration can be defined by creating a custom class and inheriting it from the ManualConfig class. Here is an example of the TestBenchmark class that we created earlier containing some benchmark methods:
[Config(typeof(Config))] public class TestBenchmark { private class Config : ManualConfig { // We will benchmark ONLY method with names with names (which
// contains "A" OR "1") AND (have length < 3) public Config() { Add(new DisjunctionFilter( new NameFilter(name => name.Contains("Recursive")) )); } } [Params(10,20,30)] public int Len { get; set; } [Benchmark] public void Fibonacci() { int a = 0, b = 1, c = 0; Console.Write("{0} {1}", a, b); for (int i = 2; i < Len; i++) { c = a + b; Console.Write(" {0}", c); a = b; b = c; } } [Benchmark] public void FibonacciRecursive() { Fibonacci_Recursive(0, 1, 1, Len); } private void Fibonacci_Recursive(int a, int b, int counter, int len) { if (counter <= len) { Console.Write("{0} ", a); Fibonacci_Recursive(b, a + b, counter + 1, len); } } }
In the preceding code, we defined the Config class that inherits the ManualConfig class provided in the benchmark framework. Rules can be defined inside the Config constructor. In the preceding example, there is a rule that stipulates that only those benchmark methods that contain Recursive should be executed. In our case, we have only one method, FibonacciRecursive, that will be executed and whose performance we will measure.
Another way of doing this is through the fluent API, where we can skip creating a Config class and implement the following:
static void Main(string[] args) { var config = ManualConfig.Create(DefaultConfig.Instance); config.Add(new DisjunctionFilter(new NameFilter(
name => name.Contains("Recursive")))); BenchmarkRunner.Run<TestBenchmark>(config); }
To learn more about BenchmarkDotNet, refer to http://benchmarkdotnet.org/Configs.htm.
- GitHub Essentials
- 數據庫程序員面試筆試真題庫
- 基于Apache CXF構建SOA應用
- 編寫有效用例
- HikariCP連接池實戰
- 區域云計算和大數據產業發展:浙江樣板
- 數據修復技術與典型實例實戰詳解(第2版)
- MySQL數據庫技術與應用
- Access數據庫開發從入門到精通
- Spring Boot 2.0 Cookbook(Second Edition)
- Microsoft Dynamics NAV 2015 Professional Reporting
- MySQL數據庫應用與管理
- 數據會說話:活用數據表達、說服與決策
- MySQL核心技術手冊
- C# 7 and .NET Core 2.0 High Performance