Visual Studio的單元測試會記錄每一個測試的執行時間,如果有幾個Method要測效能,以前我會用Stopwatch最近我都改用單元測試來測,快又簡單。

範例程式:有人說Catch不加Expection,因為不用匹配會比較快,我不相信,就來測測看。

public void Test1()
{
    for (int i = 0; i < 100000; i++)
    {
        try
        {
            var value = int.Parse("A");
        }
        catch
        {
        }
    }
}

public void Test2()
{
    for (int i = 0; i < 100000; i++)
    {
        try
        {
            var value = int.Parse("A");
        }
        catch (Exception)
        {
        }
    }            
}

Visual Studio單元測試小應用-測執行時間

好像真的快那麼一點點。

增加Duration欄位

在預設的設定下Test Results視窗是沒有Duration的欄位,必需自己增加。

Visual Studio單元測試小應用-測執行時間

在Test Results視窗上按右鈕,選擇Add/Remove Columns

Visual Studio單元測試小應用-測執行時間

選擇Duration欄位,這樣在Test Results就可以很一目了然的看到測試結果。

相关文章:

  • 2021-09-29
  • 2022-02-20
  • 2022-03-01
  • 2021-10-08
  • 2021-05-30
  • 2021-04-11
  • 2021-08-07
  • 2021-05-11
猜你喜欢
  • 2022-01-29
  • 2021-10-03
  • 2022-12-23
  • 2021-06-14
  • 2021-12-18
  • 2022-12-23
  • 2021-10-13
相关资源
相似解决方案