benchmarking code

hi,
I am trying to write a benchmark program that takes about 20 minutes to complete because the actual functions needs to be called plenty(unknown variable) times.

I have the following code
1
2
3
4
5
6
7
8
9
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=((seconds)*1000 + useconds/1000)+0.5;
 cout<<"elapsed time is:"<<mtime<<" milliseconds\n";

but I am required to write it in such a way that the output deduces the best units to use for the elapsed time and displays the results in those units.
do I need to write an if-else for that?
Yes. You need to determine the range of the number you want to display and use the appropriate format.
does that mean something like this
 
  (mtime>1000)?cout<<"elapsed time in seconds:"<<setprecision(8)<<mtime/1000<<" seconds\n":cout<<"elapsed time in milliseconds: "<<setprecision(3)<<mtime<<" milliseconds\n"; 


also, can you please tell me if mtime should be a long or int or double?
Topic archived. No new replies allowed.