日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BenchmarkDotNet的使用

發布時間:2025/5/22 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BenchmarkDotNet的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我的高級架構師告訴我,檢驗程序性能時不要用DateTime.Now相減或者StopWatch,最好用BenchmarkDotNet,于是我就試了一下。
上手體驗后感覺BenchmarkDotNet大致的特點如下:

  • 用起來還比較簡單,在對應的method上面打上[Benchmark]標記即可;
  • 通過var summary = BenchmarkRunner.Run();來啟動;
  • 要求class和method必須是public;
  • 要求程序必須是Release;
  • 會自動將程序中打標記的對應模塊跑很多遍,最后給出均值和偏差;
  • 使用前要在NuGet管理器中,或者在包管理器的PM終端中安裝BenchmarkDotNet。

    最后上代碼(以測試SeriLog和原生StreamWriter的效率對比為例):

    // See https://aka.ms/new-console-template for more information using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using Serilog; using Serilog.Core;SerilogTest.Program.main();namespace SerilogTest {public class Program{Logger _log2Console = new LoggerConfiguration().WriteTo.Console().CreateLogger();Logger _log2File = new LoggerConfiguration().WriteTo.File("seri.log").CreateLogger();private StreamWriter _sw2File = new StreamWriter("sq.log");private int _loopMaxTime = 100;[Benchmark]public double Seri2ConsoleI(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2Console.Information($"Serilog info {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Seri2ConsoleE(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2Console.Error($"Serilog error {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Seri2FileI(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2File.Information($"Serilog info {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Seri2FileE(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2File.Error($"Serilog error {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Stream2File(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_sw2File.WriteLine($"{tmpStartTime.ToString()}.077 +08:00 [ERR] Stream Writer {i}");}_sw2File.Flush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Stream2Console(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){Console.WriteLine($"{tmpStartTime.ToString()}.077 +08:00 [ERR] Console Write {i}");}DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}public static void main(){var summary = BenchmarkRunner.Run<Program>();/*double SeriConcolsErrorCost = Seri2ConsoleE();double SeriConcolsInfoCost = Seri2ConsoleI();double SeriFileErrorCost = Seri2FileE();double SeriFileInfoCost = Seri2FileI();double SystConsoleInfoCost = Stream2Console();double SystFileInfoCost = Stream2File();Console.WriteLine($"[To Console]:\n" +$" Seri info: { SeriConcolsInfoCost }ms\n" +$" Seri err: { SeriConcolsErrorCost }ms\n" +$" Sys output: { SystConsoleInfoCost }ms\n" +$"[To file]:\n" +$" Seri info: { SeriFileInfoCost }ms\n" +$" Seri error: { SeriFileErrorCost }ms\n" +$" Sys output: { SystFileInfoCost }ms\n");*/}} }

    運行結果如下:

    結果日志也會保存在新生成的BenchmarkDotNet.Artifacts目錄中。

    總結

    以上是生活随笔為你收集整理的BenchmarkDotNet的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。