I am trying to write a benchmark program that takes about 20 minutes to complete because the actual functions need to be called at least 50 times.
I used the following code:
struct timeval start, end;
long mtime, seconds, useconds;
gettimeofday(&start, NULL);
usleep(2000);
gettimeofday(&end,NULL);
seconds =end.tv_sec - start.tv_sec;
useconds=end.tv_usec - start.tv_usec;
(mtime>1000)?cout<<"elapsed time in seconds:"<<setprecision(8)<<mtime/1000<<"seconds\n":cout<<"elapsed time in milliseconds: "<<setprecision(3)<<mtime<<" milliseconds\n";
but I am required to write it in such a way t开发者_如何学Pythonhat the output deduces the best units to use for the elapsed time and displays the results in those units. any suggestions how I can revise the above code for the requirement? thx!
My guess is that your "best unit" requirement is basically "human usable units." If so, take the time and
- millisecs mod 1000 gives number of milliseconds
- millisecs div 1000 gives seconds (where div is integer division)
- seconds mod 60 gives number of seconds
- seconds div 60 gives minutes
and so on.
精彩评论