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

Exploring BenchmarkDotNet

In this section, we will explore BenchmarkDotNet and learn how effectively it can be used to measure application performance.

It can simply be installed using a NuGet package manager console window or through the Project References section of your project. To install BenchmarkDotNet, execute the following command:

Install-Package BenchmarkDotNet 

The preceding command adds a BenchmarkDotNet package from NuGet.org.

To test the BenchmarkDotNet tool, we will create a simple class that contains two methods to generate a Fibonacci series for a sequence of 10 numbers. The Fibonacci series can be implemented in multiple ways, which is why we are using it to measure which code snippet is faster and more performance efficient.

Here is the first method that generates the Fibonacci sequence iteratively:

public class TestBenchmark 
{ 
  int len= 10; 
  [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; 
    } 
  } 
} 

Here is another method that uses the recursive approach to generate the Fibonacci series:

 
[Benchmark] 
public  void FibonacciRecursive() 
{ 
  int len= 10; 
  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); 
  } 
}  

Note that both of the main methods of the Fibonacci series contain a Benchmark attribute. This actually tells the BenchmarkRunner to measure methods that contain this attribute. Finally, we can call the BenchmarkRunner from the main entry point of the application that measures the performance and generates a report, as shown in the following code:

static void Main(string[] args)
{
BenchmarkRunner.Run<TestBenchmark>();
Console.Read();
}

Once the benchmark is run, we will get the report as follows:

As well as this, it also generates files in the root folder of an application that runs the BenchmarkRunner. Here is the .html file that contains the information about the version of BenchmarkDotNet and the OS, the processor, frequency, resolution, and timer details, the .NET version (in our case, .NET Core SDK 2.0.0), host, and so on:

The table contains four columns. However, we can add more columns, which are optional by default. We can also add custom columns as well. The Method is the name of the method that contains the benchmark attribute, the Mean is the average time it takes for all the measurements to be taken (where us is microseconds), Error is the time taken to process errors, and StdDev is the standard deviation of the measurements.

After comparing both the methods, the FibonacciRecursive method is more efficient as the Mean, Error, and StdDev values are smaller than the Fibonacci method.

Other than the HTML, two more files are created, a Comma Separated Value (CSV) file and a Markdown Documentation (MD) file which contains the same information.

主站蜘蛛池模板: 台中县| 楚雄市| 宽甸| 老河口市| 五莲县| 铁岭市| 榆社县| 商洛市| 靖西县| 奈曼旗| 千阳县| 道孚县| 若羌县| 宜州市| 龙游县| 苍山县| 乐陵市| 福泉市| 同仁县| 连城县| 鹤岗市| 滨州市| 中山市| 安国市| 牡丹江市| 重庆市| 遵义县| 类乌齐县| 屯门区| 英吉沙县| 缙云县| 宁德市| 沽源县| 富平县| 正安县| 淄博市| 含山县| 克什克腾旗| 卫辉市| 新绛县| 大英县|