Requirements: In the development process, the same two function methods can meet our requirements, but for performance reasons, we will choose the best performance method for calling, how to test the performance of the code?
Performance benchmarking can help programmers compare the performance of two code snippets or methods, which can provide a good quantification standard for code rewriting or refactoring. Without performance benchmarks, it's hard to imagine how you can tell the difference in performance with the naked eye when you change method A to method B.
BenchmarkDotNet is a powerful .Net performance benchmark library on the official websiteThe hyperlink login is visible.
First, we'll create a new .NET Core 3.1 console project. The project name is: BenchmarkTest
Install BenchmarkDotNet using the nuget command, the code is as follows:
Double-click the project to modify the target platform of the project, as follows:
We test the performance of the TestMD5 and TestSHA1 methods under the .NET Framework 4.7.2 and .NET Core 3.1 frameworks, respectively.
The code is as follows:
Attempt to execute it with the following error:
// Validating benchmarks: Assembly BenchmarkTest which defines benchmarks is non-optimized Benchmark was built without optimization enabled (most probably a DEBUG configuration). Please, build it in RELEASE. If you want to debug the benchmarks, please seeThe hyperlink login is visible.
Switch the project to RELEASE modeto start the project again, as shown below:
// * Summary *
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.1379 (1909/November2018Update/19H2) Intel Core i5-8259U CPU 2.30GHz (Coffee Lake), 1 CPU, 8 logical and 4 physical cores .NET Core SDK=5.0.103 [Host] : .NET Core 3.1.12 (CoreCLR 4.700.21.6504, CoreFX 4.700.21.6905), X64 RyuJIT .NET 4.7.2 : .NET Framework 4.8 (4.8.4300.0), X64 RyuJIT .NET Core 3.1 : .NET Core 3.1.12 (CoreCLR 4.700.21.6504, CoreFX 4.700.21.6905), X64 RyuJIT
| Method | Job | Runtime | Mean | Error | StdDev | Min | Max | Median | |--------- |-------------- |-------------- |---------:|----------:|----------:|---------:|---------:|---------:| | TestMD5 | .NET 4.7.2 | .NET 4.7.2 | 4.119 us | 0.0804 us | 0.1045 us | 3.958 us | 4.318 us | 4.123 us | | TestSHA1 | .NET 4.7.2 | .NET 4.7.2 | 4.043 us | 0.0793 us | 0.1085 us | 3.926 us | 4.289 us | 4.003 us | | TestMD5 | .NET Core 3.1 | .NET Core 3.1 | 1.216 us | 0.0122 us | 0.0114 us | 1.202 us | 1.238 us | 1.214 us | | TestSHA1 | .NET Core 3.1 | .NET Core 3.1 | 1.307 us | 0.0186 us | 0.0165 us | 1.284 us | 1.336 us | 1.307 us |
// * Hints * Outliers TestContext.TestSHA1: .NET 4.7.2 -> 1 outlier was removed (5.52 us) TestContext.TestSHA1: .NET Core 3.1 -> 1 outlier was removed (1.37 us)
// * Legends * Mean : Arithmetic mean of all measurements Error : Half of 99.9% confidence interval StdDev : Standard deviation of all measurements Min : Minimum Max : Maximum Median : Value separating the higher half of all measurements (50th percentile) 1 us : 1 Microsecond (0.000001 sec)
// ***** BenchmarkRunner: End ***** // ** Remained 0 benchmark(s) to run ** Run time: 00:01:14 (74 sec), executed benchmarks: 4
Global total time: 00:01:20 (80.63 sec), executed benchmarks: 4 // * Artifacts cleanup * Once executed, the test results are saved to our project \BenchmarkTest\bin\Release\netcoreapp3.1\BenchmarkDotNet.Artifacts\results folder, in csv, html, md, and other formats.
Note:SimpleJob target frame selection is incorrect and may not execute properlyAs follows:
Failed to build in Parallel, switching to sequential build
Resources:The hyperlink login is visible.
|