How to calculate CPU usage of a piece of .NET code?

Our tool generates performance logs in diagnostic mode, however we monitor performance as during code execution (stopwatch + milliseconds).

Obviously, it is not reliable at all, the processor of the testing system can be used by some random process, the results will be completely different if you tuned the tool to 10 threads instead of 2, etc.

My question is:

What is the right way to find out the correct processor time for a piece of code (and not for the whole process)?

What do I mean by processor time:

Basically, how many CPU cycles. I assume that this will always be the same for the same piece of code on the same computer and will not be executed by other processes. There may be some fundamental things that I am missing here, if so, please enlighten me in the comments or answers.

PS Using the profiler is not possible in our setup

Another update

Why am I not going to use a profiler?

Because we need to test the code in different environments with different data, where we do not have a profiler or IDE or something like that. Therefore, the code itself must handle this. An extreme option might be to use a DLL profiler, perhaps, but I donโ€™t think that this task requires such a complicated solution (provided that there is no free and easy to use profiling library).

+6
performance optimization cpu-usage
source share
3 answers

I assume that this will always be the same for the same piece of code on the same computer and will not be executed by other processes.

This is not how computers work. The code is very much affected by other processes running on the machine. A typical Windows machine contains about 1000 active threads; you can see them on the Performance tab of Taskmgr.exe. The vast majority of them are sleeping, waiting for some kind of event that Windows signals. Nevertheless, if a code is running on the computer, including yours, which is ready to go and take CPU time, then Windows will provide them with a whole piece of the pie.

Which makes measuring the time taken by your code a rather arbitrary measurement. The only thing you can appreciate is the minimum time. What you do, checking dozens of times, is the likelihood that you will get a sample that was not affected by other processes. However, this will never happen in real life, it would be wise for you to perceive the median value as a realistic dimension of performance.

The only really useful measurement is to measure additional improvements to your algorithm. Change the code, see how the median time changes because of this.

+3
source share

Basically, how many CPU cycles. I assume that this will always be the same for the same piece of code in the same computer and not executed by other processes. Perhaps some fundamental material that I am missing here, if so, please enlighten me in the comments or answers.

The processor time used by the function is a really squishy concept.

  • Does it include I / O running anywhere below it in the call tree?
  • Is this just โ€œpersonal timeโ€ or does it include calls? (In serious code, self-start is usually zero.)
  • Is he averaged over all the challenges? Define "everything."
  • Who consumes this information, for what purpose? Find the so-called bottlenecks? Or just some regression tracking purpose?

If the goal is not just measurement, but also the search for code that is worth optimizing, I think a more useful concept is Percentage of time on the stack . An easy way to collect this information is to read a stack of function calls during the random hours of the wall clock (during the interval you care about). This property has the following properties:

  • It shows the percentage time as a percentage.
  • It gives a percentage of the linear level (and not just the function level), so it points to expensive lines of code, regardless of whether they are function calls.
  • It includes input / output operations as well as processor time. (In serious software, it is not practical to exclude I / O time, because you really don't know what the time spends several layers lower in the call tree.)
  • This is relatively insensitive to competition for the processor by other processes or to processor speed.
  • Finding an expensive code does not require a high sampling rate or a large number of samples. ( This is a common misconception. ) Each additional digit of measurement accuracy requires about 100 times more samples, but this does not more accurately determine the more expensive code.

A profiler that works on this principle is Zoom .

On the other hand, if the goal is just to measure, the user can see if changes in the work helped or hurt performance, then you need to control the processor environment, and a simple general time measurement is what I recommend.

+1
source share

The best way to measure processor time is to use the "rdtsc" or "Read Time Stamp Counter" instruction. This counter (part of the CPU itself) increases the internal clock speed of the processor. Thus, the difference between the two readings is the number of clock cycles that have passed. This counter can be integrated into your code if it (the code) is not too high-level (although not entirely sure). You can measure disk time, network time, CPU time, etc. - the possibilities are endless. If you divide the number of past ticks with the processor frequency in megahertz, you get, for example, the elapsed number of microseconds. Possible such good accuracy and better. Contains the creation of a graphical interface that will interact with your processor usage statistics.

Search for "rdtsc" or "rdtsc" or "_rdtsc" in the help files for your environment.

+1
source share

All Articles