i had interesting discussion friend benchmarking c/c++ code (or code, in general). wrote simple function uses getrusage
measure cpu time given piece of code. (it measures how time of cpu took run specific function). let me give example:
const int iterations = 409600; double s = measurecpu(); for( j = 0; j < iterations; j++ ) function(args); double e = measurecpu(); std::cout << (e-s)/iterations << " s \n";
we argued, should divide (e-s) number of iterations, or not? mean, when dont divide result in acceptable form (ex. 3.0 s) when divide it, gives results 2.34385e-07 s ...
so here questions:
- should divide (e-s) number of iterations, if so, why?
- how can print 2.34385e-07 s in more human-readable form? (let's say, took 0.00000003 s) ?
should first make function call once, , after measure cpu time iterations, this:
// first function call, doesnt bother @ function(args); // real benchmarking const int iterations = 409600; double s = measurecpu(); for( j = 0; j < iterations; j++ ) function(args); double e = measurecpu(); std::cout << (e-s)/iterations << " s \n";
- if divide time number of iterations, you'll iteration independent comparison of run time of 1 function, more iterations, more precise result. edit: average run time on n iterations.
you can multiply divided time 1e6 microseconds per 1 iteration unit (i assume measurecpu returns secods)
std::cout << 1e6*(e-s)/iterations << " s \n";
as @ogni42 stated, getting overhead loop measured time, try unroll loop bit lower measurement error, 8 16 calls each iteration, try different call counts see how measured time changes:
for( j = 0; j < iterations; j++ ) { function(args); function(args); function(args); function(args); ... }
what lower better number. if wanted higher better scoring measure diferent variations of function , time of fastest one. 1 score 10 points.
score_for_actual_function = 10.0 * fastest_time / time_of_actual_function
this scoring kind of time independent, can compare different function variations , function can score less 1 point... , beware of division 0 :)
Comments
Post a Comment