📜 ⬆️ ⬇️

Performance Optimization in C ++ Applications (Windows)

I think everyone already knows, but still. Sometimes it is necessary to estimate how much processor time it takes to perform certain functions of the program (mainly I am speaking now about C ++). Well, below I bring my own way, simple, but it suits me perfectly.

You can instrument the code with various special tools (I have not tried it, like DevPartner is one of them, I think there are a lot of them now).
Functions associated with time do not work very well, I did not find them in WinAPI or in any other API.

Two functions that I really liked:
')
QueryPerformanceFrequency
QueryPerformanceCounter

In general, here you can finish reading the article, a little google and enough :)

With the help of this pair of functions, it is possible to obtain fairly accurate estimates of the running time of software calculations.

Actually, QueryPerformanceFrequency returns the number of elementary operations that your processor can perform in one second.
QueryPerformanceCounter returns the number of elementary operations that it has already performed since the computer was started.

“GetTickCount” seems to be an alternative, but I somehow didn’t have enough accuracy, or I wasn’t satisfied with its behavior, which wasn’t clearly described in MSDN, rather it can only be used for UI calculations.

Calculate how many seconds it takes to call any function (class method) DoWork () using QueryPerformanceFrequency / QueryPerformanceCounter is actually quite simple:

 LARGE_INTEGER performanceCounter;
 :: QueryPerformanceFrequency (& performanceCounter);

 LARGE_INTEGER performanceCounterStart;
 :: QueryPerformanceCounter (& performanceCounterStart);

 DoWork ();

 LARGE_INTEGER performanceCounterEnd;
 :: QueryPerformanceFrequency (& performanceCounterEnd);

 double timeElapsed = (double *) (performanceCounterEnd) - (double *) (performanceCounterEnd)



Sinful code of course, I just showed the idea, maybe all of a sudden it will come in handy.
In general, QueryPerformanceCounter, this is the “ReaD Time Stamp Counter (RDTSC)” assembler instruction (I lie about 5, the instruction name is # 0f, # 31, although I am ashamed to say what processors it works on, for which I ask you to forgive :)

Shl. When evaluating performance and the simplest tests, with which I sometimes use, you should pay attention to the compiler, most of the sane compilers have a code block like

 CalculateSinus (cobst Double & x)
 {
     return sin (x);
 }

 static const double x = PI / 4;
 double y = CalculateSinus (x)


convert so that in general it will not need to be considered at run time, and y = sin (PI / 4) will be calculated at the compilation stage.

Source: https://habr.com/ru/post/56228/


All Articles