From: Maanu on
Hi,

My c# application take more than 60% of CPU utilization. I would like to
know which methods are CPU intensive.

Is it possible to identify that?

Thanks!
From: Peter Duniho on
Maanu wrote:
> Hi,
>
> My c# application take more than 60% of CPU utilization. I would like to
> know which methods are CPU intensive.
>
> Is it possible to identify that?

If your code is executing, it's CPU intensive. The only way you avoid
being CPU intensive is to wait for something else (e.g. reading from a
file, user input, network i/o, etc.)

While executing, _all_ of your code is "CPU intensive". You only reduce
your demand on the CPU by not causing the code to be executing as
frequently or for as much time.

Note that to the extent that a program has been implemented
inefficiently, that can artificially and needlessly increase CPU
utilization. That is, if you write code that causes the CPU to have to
spend 500 milliseconds to accomplish something that should take only 50
milliseconds, then of course CPU utilization will be higher than it
needs to be.

If you have a specific concise-but-complete code example that reliably
demonstrates a CPU utilization problem, then perhaps some additional
commentary can be provided. Otherwise, there's not much else to offer
beyond the above.

Inasmuch as high CPU utilization could be related to the code spending
more time than is necessary in a particular area of the code, a profiler
such as that provided with the more expensive versions of Visual Studio
can be helpful.

If you don't have access to a profiler, then if you're patient enough
and willing to do some tedious work, you can accomplish much of the same
results simply by adding Stopwatch object instances to your code to
measure how long specific methods take. Start at the top and measure
each method call in a top-level method; whatever method is taking the
longest, repeat the procedure within that method, and so on.

Pete