- 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.
- 我們都是數據控:用大數據改變商業、生活和思維方式
- MySQL高可用解決方案:從主從復制到InnoDB Cluster架構
- 云數據中心基礎
- Greenplum:從大數據戰略到實現
- Unity 5.x Game AI Programming Cookbook
- 云計算環境下的信息資源集成與服務
- 算法與數據中臺:基于Google、Facebook與微博實踐
- Power BI商業數據分析完全自學教程
- 高維數據分析預處理技術
- Flutter Projects
- 大數據治理與安全:從理論到開源實踐
- IPython Interactive Computing and Visualization Cookbook(Second Edition)
- 一本書講透Elasticsearch:原理、進階與工程實踐
- Oracle 內核技術揭密
- Unity for Architectural Visualization