Rafael Souza
Rafael Souza

Reputation: 27

CPU processing in the code

How do I know how much CPU processing is using a certain routine in my code. This would be possible? I can pick up using this as my program ... using:

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

But have some way to get only a specific routine?

Upvotes: 0

Views: 127

Answers (2)

spender
spender

Reputation: 120380

Unless your code is waiting on IO, the CPU usage will be around 100% (of the core it's running on), right?

Upvotes: 0

Dan Pichelman
Dan Pichelman

Reputation: 2332

I suspect a proper answer will include a write up on the performance monitoring tools in the latest Visual Studio. In the mean time, I wonder if the Stopwatch class might be useful?

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

Upvotes: 2

Related Questions