A class for measuring periods of elapsed time. Can also accrue split times for some basic statistical reporting.
More...
#include <timer.hpp>
Comparisons between two timer_t
considers only the last split time.
- Model Of
-
- Rationale
- A class that measures elapsed time can be useful when debugging, optimizing, or comparing implementations.
timer_t
utilizes the most accurate timing API a platform has to offer to maximize accuracy, while keeping the public API generic.
- Example
- Checking the speed between two implementations might look like this:
do_my_func();
timer1.split();
do_their_func();
timer2.split();
if (timer1 == timer2)
std::cout << "Functions are equally fast" << std::endl;
else if (timer1 < timer2)
std::cout << "My function is faster" << std::endl;
else
std::cout << "Their function is faster" << std::endl;
A class for measuring periods of elapsed time. Can also accrue split times for some basic statistical...
- Most of the time, however, you will want to take several measurements and compare the averages in order to gain a more accurate understanding of the cost of an implementation:
for (std::size_t i(0); i < sample_count_k; ++i)
{
timer1.reset();
do_my_func();
timer1.accrue();
timer2.reset();
do_their_func();
timer2.accrue();
}
double my_avg(timer1.accrued_average());
double their_avg(timer2.accrued_average());
if (my_avg == their_avg)
std::cout << "Functions are equally fast" << std::endl;
else if (my_avg < their_avg)
std::cout << "My function is faster" << std::endl;
else
std::cout << "Their function is faster" << std::endl;
- Note that in the above case the two implementations are tested in the same
for
loop. This is in attempt to distribute possible overhead from memory locality issues (i.e., do_my_func
could push memory used by do_their_func
out of the cache, unfairly slowing do_their_func
down (or vice versa).
- Depending on what you are measuring in your own code, measurement strategies may vary from the ones used above.