This article is a mirror article of machine translation, please click here to jump to the original article.

View: 33025|Reply: 2

[Communication] .NET/C# uses BenchmarkDotNet to test code performance

[Copy link]
Posted on 3/3/2021 3:51:12 PM | | | |
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.




Previous:Beyond Compare Solution to the error "This authorization key has been revoked"
Next:Java calls to the C# webservice server fail to recognize the value of the HTTP header SOAPAction
 Landlord| Posted on 3/31/2022 3:56:47 PM |
1. Mean translation: mean, usually refers to the arithmetic average, or geometric average.

Narrative: Add up all the values in a set of sequences and divide by the total number, which is the mean.

2. Median translation: intermediate value.

Narrative: Arrange a set of sequences in ascending order, and then take the middle value, which is the middle value.
 Landlord| Posted on 5/25/2023 8:13:33 PM |
.NET/C# uses Span to handle strings with high performance
https://www.itsvse.com/thread-10596-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com