CPU处理中的代码

本文关键字:代码 处理 CPU | 更新日期: 2023-09-27 18:10:06

我如何知道在我的代码中使用某个例程有多少CPU处理。这可能吗?我可以拿起这个作为我的程序…使用:

CpuUsage _cu = new CpuUsage();
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_total";
string cpuUsage = _cu.GetUsage();

但是有办法只得到一个特定的例程吗?

CPU处理中的代码

我想一个合适的答案应该包括写一篇关于最新Visual Studio中的性能监控工具的文章。同时,我想知道秒表课程是否有用?

var stopWatch = new System.Diagnostics.Stopwatch.StartNew();
myFunctionToBeTimed();
Console.Writeline(stopWatch.ElapsedTicks.ToString());

除非您的代码正在等待IO,否则CPU使用率将在100%左右(它正在运行的核心),对吗?