In this article, I will explain How to benchmark C# code
using BenchmarkDotNet
BenchmarkDotNet is a lightweight, open-source, powerful .NET library that can transform your methods into benchmarks, track those methods,
and then provide insights into the performance data captured.
It is easy to write BenchmarkDotNet benchmarks and the
results of the benchmarking process are user friendly as well.
Why benchmark code?
A benchmark is a measurement or a set of measurements related to the performance of a piece of code in an application.
Benchmarking code is essential to understanding the performance metrics of the methods in your application. It is always a good approach to have the metrics at hand when you’re optimizing code. It is very important for us to know if the changes made in the code have improved or worsened the performance.
Benchmarking also helps you to narrow in on the portions of the code in the application that needs refactoring.
Create a console application project in Visual Studio
First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio.
- Launch the Visual Studio IDE.
- Click on “Create a new project.”
- In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
- Click Next.
- In the “Configure your new project” window shown next, specify the name and location for the new project.
- Click Create.
Install the BenchmarkDotNet NuGet package
To work with BenchmarkDotNet you must install the BenchmarkDotNet package. You can do this either via the NuGet Package Manager inside the Visual Studio 2019 IDE, or by executing the following command at the NuGet Package Manager Console:
Steps for benchmarking code using BenchmarkDotNet
To run BenchmarkDotNet in your .NET Framework or .NET Core application you must follow these steps:
- Add the necessary NuGet package
- Add Benchmark attributes to your methods
- Create a BenchmarkRunner instance
- Run the application in Release mode
Create a benchmarking class in .NET Core
Run the benchmark in your .NET Core application
If you run the application in debug mode, here’s the error message you’ll see:
Hence you should run your project in the release mode only. To run benchmarking, specify the following command at the Visual Studio command prompt.
Analyze the benchmarking results
Once the execution of the benchmarking process is complete, a summary of the results will be displayed at the console window. The summary section contains information related to the environment in which the benchmarks were executed, such as the BenchmarkDotNet version, operating system, computer hardware, .NET version, compiler information, and information related to the performance of the application.
A few files will also be created in the BenchmarkDotNet.Artifacts folder under the application’s root folder. Here is a summary of the results.
Happy programming!!
Don’t forget to leave your feedback and comments below!
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
Blog: www.sujeetbhujbal.com
Personal
Website :-http://sujeetbhujbal.wordpress.com/
CodeProject:-http://www.codeproject.com/Members/Sujit-Bhujbal
CsharpCorner:-http://www.c-sharpcorner.com/Authors/sujit9923/sujit-bhujbal.aspx
Linkedin :-http://in.linkedin.com/in/sujitbhujbal
Twitter
:-http://twitter.com/SujeetBhujbal
------------------------------------------------------------------------------
on-demand ecommerce app development services
ReplyDeleteThis is so helpful to me. Keep updating more articles.
ReplyDeleteUbs accounting
Myob Singapore
Best Accounting software Singapore
on-demand car wash app development company
ReplyDeleteBetway sign up offer, registration, deposit bonus & free bet
ReplyDelete› online › › online › Betway sign up offer, registration, deposit 안산 출장마사지 bonus & free bet Betway Sign Up Offer 서울특별 출장샵 - Register at betway 계룡 출장마사지 in Betway and play with 거제 출장안마 other players. Register Now. 영주 출장안마
I generally check this kind of article and I found your article which is related to my interest. Genuinely it is good and instructive information. Thankful to you for sharing an article like this.Self-Ordering Kiosk Singapore
ReplyDelete