linux - Erratic average execution times for C function -
i'm trying optimize chunk of code given me friend baseline average execution times of extremely erratic , i'm lost why/how fix it.
code:
#include <sys/time.h> #include <time.h> #include <stdio.h> #include "wall.h" /* code */ int main() { int average; struct timeval tv; int i; for(i = 0; < 1000; i++) /* running code 1,000 times */ { gettimeofday(&tv, null); /* starting time */ start(); /* launching code */ int ret = tv.tv_usec; /* finishing time */ ret /= 1000; /* converting milliseconds */ average += ret; /* adding average */ } printf("average execution time: %d milliseconds\n", average/1000); return 0; }
output of 5 different runs:
- 804 milliseconds
- 702 milliseconds
- 394 milliseconds
- 642 milliseconds
- 705 milliseconds
i've tried multiple different ways of getting average execution time, each 1 either doesn't give me precise enough answer or gives me erratic one. i'm lost now, appreciated!
i know these types of benchmarks system dependent, i've listed system specs below:
- ubuntu 12.10 x64
- 7.8 gib ram
- intel core i7-3770 cpu @ 3.40ghz x 8
- geforce gt 620/pcie/sse2
edit
thank input decided go gprof instead of constructing own. thank you, once again!
there several problems here, including 0 details on code you're benchmarking doing, , you're using "gettimeofday()" incorrectly (and, perhaps, inappropriately).
suggestions:
1) don't use "gettimeofday()":
http://blog.habets.pp.se/2010/09/gettimeofday-should-never-be-used-to-measure-time
2) supplement "time elapsed" gprof:
Comments
Post a Comment