By performance timers do you mean getting the time delta from the start of the run and the end of the run? If so, don't use ctime/time.h for gathering the elapsed time, it is inaccurate. Instead, you have to use <sys/time.h> in linux to gather the elapsed time.
The way how it works:
timeval structure and gettimeofday(timeval &) gives microsecond accuracy, which is then truncated to milliseconds which should be precise enough. At least has been for my use.Code:#include <iostream> #include <sys/time.h> void init_timer(timeval &tv) { /* Update tv to the current time */ gettimeofday(&tv, 0); }; unsigned int get_time(timeval &start, timeval &end) { /* Update end to current time, at this point start should be updated earlier by init_timer() */ gettimeofday(&end, 0); /* Calculate the difference in microseconds(tv_usec) and convert it to milliseconds, do the same to seconds(tv_sec) */ return (end.tv_usec-start.tv_usec)/1000 + (end.tv_sec-start.tv_sec)*1000; }; int main() { timeval tv_start; timeval tv_end; init_timer(tv_start); /* Do something */ std::cout << get_time(tv_start, tv_end) << std::endl; return 0; };
The good thing about sys/time.h is that it is part of C POSIX library, so it should work with any POSIX compicant operating systems.




Reply With Quote


Bookmarks