performance measure for optimized c++ code

G

gautamcool88

I want to measure the execution time of a function, which is in the
order of microseconds. The approach I am using right now is to call
the function a sufficient number of times so that the cumulative time
is measurable, then divide the cumulative time by the number of times
the function was called.

I have some doubts about this approach -
With full compiler optimizations, I am not sure if the compiler is
making use of the facts that
(a). the input arguments to the function remain the same (and hence
the output is also the same) each time the function is called.
(b) that I am not making use of the output. (not printing to a file/
stdout).

So the compiler might be executing the function partially or in a
different manner. I do not want that, as it will obviously give a
misleading results.

At the same time, I do want "normal" optimizations. (Which do not make
use of points (a) and (b) mentioned above). Any suggestions for
this?

(The function can not contain any cout statements because that would
severely change its running time.)
 
D

Dijkstra

I want to measure the execution time of a function, which is in the [...]
I have some doubts about this approach -
With full compiler optimizations, I am not sure if the compiler is
making use of the facts that [...]
So the compiler might be executing the function partially or in a
different manner. I do not want that, as it will obviously give a
misleading results.

The compiler does not "execute" nothing. But I understand what you
mean. The compiler only optimizes what it can see at compile time,
nothing more, nothing less. You can separate your target function and
the loop in two distinct modules, so you will be pretty sure the
compiler will not fold the loop.

Cheers,
Dijkstra.
 
J

James Kanze

On 9 jul, 15:16, "(e-mail address removed)" <[email protected]>
wrote:
I want to measure the execution time of a function, which is in the [...]
I have some doubts about this approach -
With full compiler optimizations, I am not sure if the compiler is
making use of the facts that [...]
So the compiler might be executing the function partially or in a
different manner. I do not want that, as it will obviously give a
misleading results.
The compiler does not "execute" nothing. But I understand what you
mean. The compiler only optimizes what it can see at compile time,
nothing more, nothing less. You can separate your target function and
the loop in two distinct modules, so you will be pretty sure the
compiler will not fold the loop.

Unless you're using Sun CC. Or VC++. Or any one of a number of
other compilers which can optimize across translation
boundaries. (Of course, you usually need special options for
such optimization.)

My own solution has been to make the function virtual, which
seems to suffice for the compilers I currently use (but I have
no guarantees for the future). And I do ensure that the
function "does something" which affects global state. And that
the same call point uses different types of objects (and thus
resolves to a different function) at different times in the
execution. (Amongst other things, I make a dry run, with an
empty virtual function, to establish a base value, which is then
subtracted from all of the other run times.)

The only way to be sure, of course, is to look at the code the
compiler is generating, and determine manually if the
optimizations it is doing are falsifying the measurement. And
to be cleverer than the authors of the compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top