compute the average execution time for a function

hello,
I wrote a simple function in a c++ program which I call several times in a program run.

I implemented it both in serial and parallel (using openMP) version. The execution time vary by input even in the serial implementation (and this is correct).
I know how to time some parts of my code using boost::timer, but I don't know how to average these values.

My goal is to compute the average execution time for the function, both the serial and the parallel version.



first thing is to not run your function in a tight loop. The CPU/OS /something can cache the work being done and tweak it so that subsequent runs are faster than the inital run. It is better to either run it 'naturally' the way it would in the real program, which you can also do by adding some fluff, eg
1
2
3
4
5
6
7
for(x = 0; x < 10000; x++) 
{
    callfluff1();
     timefunction();
    callfluff2();
}

but the fluff functions need to DO something meaningful. Maybe make a very large random vector and sort it or something. If your real use-case is to run it in a tight loop, you don't need this; its ok to get the optimized number in that case as it represents reality.

as for the timer, just add time and divide by # of calls.

1
2
3
4
5
6
7
8
9
foo()
{ 
   static double totaltime= 0.0;
    x = start_time();
    code();
    totaltime += now-x;  //return this 
}
...
avg time = totaltime/numruns; //etc 

Last edited on
Topic archived. No new replies allowed.