官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 温泉县| 塔河县| 安溪县| 东乌珠穆沁旗| 滦南县| 县级市| 安泽县| 嵩明县| 梁山县| 绥德县| 吉水县| 仙居县| 吉木乃县| 阿巴嘎旗| 漳浦县| 三河市| 石楼县| 凭祥市| 深水埗区| 宁津县| 黄陵县| 中卫市| 广丰县| 威海市| 马公市| 苍溪县| 六安市| 日照市| 赣榆县| 宿迁市| 蒙阴县| 昌平区| 海兴县| 淳安县| 宁城县| 安康市| 镇雄县| 安岳县| 纳雍县| 年辖:市辖区| 舒兰市|