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

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.

主站蜘蛛池模板: 高平市| 琼海市| 湖北省| 当涂县| 镇赉县| 乌鲁木齐县| 富阳市| 阿拉善左旗| 东山县| 新沂市| 大名县| 庆城县| 临颍县| 孟连| 岗巴县| 邓州市| 府谷县| 鄂州市| 宜君县| 汶上县| 酒泉市| 民乐县| 江永县| 萨嘎县| 健康| 冷水江市| 丰原市| 平原县| 东安县| 开远市| 汉中市| 阿克| 拉孜县| 瑞丽市| 江口县| 衡阳市| 富源县| 海晏县| 湾仔区| 稻城县| 夹江县|