1 安装BenchmarkDotNet 包
2 写两个算法 方法 sha256 和sha512 标记特性
[MaxColumn,MinColumn,MemoryDiagnoser] public class ShaTest { Testa Testa=new Testa(); [Benchmark] public string Sha256A() => Testa.Sha256("http://www.cndns.com"); [Benchmark] public string Sha512B() => Testa.Sha512("http://www.cndns.com"); }
3 方法入口处 添加方法
// See https://aka.ms/new-console-template for more information using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using Cndns.Test; BenchmarkRunner.Run<ShaTest>(); Console.WriteLine("ok");
4 Release 模式下 ctrl+ F5
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19041.1348 (2004/May2020Update/20H1)
Intel Core i5-4200U CPU 1.60GHz (Haswell), 1 CPU, 4 logical and 2 physical cores
.NET SDK=6.0.100
[Host] : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT [AttachedDebugger]
DefaultJob : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
| Method | Mean | Error | StdDev | Median | Max | Min | Gen 0 | Allocated |
|-------- |---------:|----------:|----------:|---------:|---------:|---------:|-------:|----------:|
| Sha256A | 1.975 us | 0.0809 us | 0.2372 us | 1.877 us | 2.589 us | 1.699 us | 0.2518 | 400 B |
| Sha512B | 2.531 us | 0.2173 us | 0.6406 us | 2.227 us | 3.979 us | 1.767 us | 0.3510 | 552 B |
明显 sha256 用时少。。。