Re: Execution time of code?
* Victor Bazarov:
mlt wrote:
I have some code that implements various seach and sorting algorithms.
I would like to get some kind of time measure for various parts of the
algorithm, like:
public myAlgo() {
...
...
float timer = // start measurement timer
for (...)
{
// do various calculations.
}
std::cout << "time spend = " << timer;
float timer2 = // start measurement timer
for (...)
{
// do some other calculations.
}
std::cout << "time2 spend = " << timer2;
...
...
}
Is there some build in function in C++ that is designed for this kind
of purpose? I am also interested in knowing if there exists some
performance measuring framework for this kind of task.
There is 'clock()', but know that it's the last function you actually
want to use to measure the performance of your code.
Why do you think that?
'clock' is extremely easy to use, and it's always available.
Hence, I'd say it's the /first/ you should try, absolutely not the last (if
other more heavy instruments are brought to bear, then 'clock' adds nothing).
To use 'clock', call the relevant piece of code an appropriate number of times
to get well within 'clock' resolution, and check how it fared.
This will often be enough to form a good opinion about rough performance, and
usually that's the best one can hope for anyway, no matter how sophisticated
instruments are employed (because detailed performance depends on data, machine
load, usage patterns, and factors that one could never imagine offhand).
Using 'clock' involves some work in adding intrusive code and/or factoring out
the relevant code to be measured.
Using a heavier instrument, unless one already has everything set up for that
instrument (which makes the question of choosing moot), involves even more work.
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to [http://alfps.izfree.com/].
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!