使用 BenchmarkDotnet 測試代碼性能


先來點題外話,清明節前把工作辭了(去 tm 的垃圾團隊,各種拉幫結派、勾心斗角)。這次找工作就得慢慢找了,不能急了,希望能找到個好團隊,好崗位吧。順便這段時間也算是比較閑,也能學習一下和填掉手上的坑。

說實話好久沒寫博客了,一個是手上的工作確實忙,第二個是還有各種各樣的坑。寫本文的原因也是因為手上的一個坑——ImageEx,WPF/UWP 上的圖片緩存控件。

 

在我寫的這個圖片緩存控件中,其中有一個地方就是要根據圖片的 url 地址,然后來存儲或者獲取本地的圖片文件的。但是呢,我們不可能把 url 當作本地文件的文件名的,一個是可能包含非法字符(如斜杠),另一個是長度可能超出限制。想了一下,那就只能用哈希(hash)來解決了,其中 MD5 和 SHA1 兩種算法我覺得都可以解決這個問題。但問題是,哪一個更好、更快呢?傳統經驗告訴我是 MD5,但是我覺得還是有必要手動實踐一下,畢竟沒有 100% 的把握。

先編寫出如下的代碼:

public static class HashHelper
{
    public static string GetMD5(string input)
    {
        if (input == null)
        {
            throw new ArgumentNullException(nameof(input));
        }

        using (var md5 = MD5.Create())
        {
            var buffer = Encoding.UTF8.GetBytes(input);
            var hashResult = md5.ComputeHash(buffer);
            return BitConverter.ToString(hashResult).Replace("-", string.Empty);
        }
    }

    public static string GetSHA1(string input)
    {
        if (input == null)
        {
            throw new ArgumentNullException(nameof(input));
        }

        using (var sha1 = SHA1.Create())
        {
            var buffer = Encoding.UTF8.GetBytes(input);
            var hashResult = sha1.ComputeHash(buffer);
            return BitConverter.ToString(hashResult).Replace("-", string.Empty);
        }
    }
}

作用是輸入一個字符串,輸出一個哈希后的字符串。

 

建立一個 .net core 的控制台項目,我就叫 TestBenchmarkDotnet。

然后安裝 nuget 包,BenchmarkDotnet。

QQ截圖20180408222314

安裝完成后編寫如下代碼:

public class TestContext
{
    [Benchmark]
    public void TestMD5()
    {
        HashHelper.GetMD5("https://www.baidu.com/img/bd_logo1.png");
    }

    [Benchmark]
    public void TestSHA1()
    {
        HashHelper.GetSHA1("https://www.baidu.com/img/bd_logo1.png");
    }
}

然后修改 Main 方法:

public class Program
{
    public static void Main(string[] args)
    {
        Summary summary = BenchmarkRunner.Run<TestContext>();
        Console.ReadLine();
    }
}

最后將 Debug 調成 Release 模式,不調試啟動。

稍微等待一會兒就會出現結果了。

QQ截圖20180408223415

結論是 MD5 確實比 SHA1 快。

另外由於這是在 .net core 下的測試結果,而 WPF 是跑在 .net framework 下的,那么是否結果可能不一樣呢?

Benchmark 支持多個 .net 環境的性能測試(.net framework, net core, mono)。

修改 TestContext 類的代碼:

[ClrJob, CoreJob]
public class TestContext
{
    [Benchmark]
    public void TestMD5()
    {
        HashHelper.GetMD5("https://www.baidu.com/img/bd_logo1.png");
    }

    [Benchmark]
    public void TestSHA1()
    {
        HashHelper.GetSHA1("https://www.baidu.com/img/bd_logo1.png");
    }
}

添加了 ClrJob 和 CoreJob 兩個標簽

然后修改項目的 csproj 文件

<TargetFramework>netcoreapp2.0</TargetFramework>

一行改為

<TargetFrameworks>netcoreapp2.0;net471</TargetFrameworks>

回到 VS 重新編譯,還原 nuget 包。

不調試啟動。稍等片刻。

QQ截圖20180408230358

可見在 .net framework 環境下,仍然是 MD5 比 SHA1 快的。而且可以看見 .net core 比 .net framework 環境下快了很多。

另外在輸出目錄下,BenchmarkDotnet 會輸出性能測試結果文件:

QQ截圖20180408230657

打開 html 版本后看到的跟剛才控制台的是一樣的

QQ截圖20180408230743


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM