2026年8月24日 星期一

[.NET/Visual Studio] 如何使用Visual Studio Performance Profiler來找耗時操作?

 前言:

系統的優化最常見的是降低操作的執行時間,Visual Studio(以下簡稱VS)內建的profiler其一的CPU Usage是很適合的入門工具。


使用方式:

  • 打開VS目錄 -> [Debug] ->[Performance Profiler](或快捷鍵Alt + F12):


  • 此例目標選擇「啟動專案」,選擇Release build / CPU Usage / Start with collection paused(執行profiler後自己啟動錄製) -> Start:

  • 在要量測的執行前,按下Record,執行後,按下Stop Collection,即可產出分析結果







  • 報表主要分為3個部分
    • Top Insights:會列出主要問題,並可搭配copilot分析或優化
    • Top Functions:依序列出耗時的函式
    • Hot Path:可列出最耗時的執行路徑



To be continued...


參考資料:

https://learn.microsoft.com/en-us/visualstudio/profiling/cpu-usage?view=visualstudio

https://learn.microsoft.com/en-us/visualstudio/profiling/running-profiling-tools-with-or-without-the-debugger?view=visualstudio

https://learn.microsoft.com/en-us/visualstudio/profiling/cpu-insights?view=visualstudio