2

I want to time the real-time performance of some C++ functions I have written. How do I get the timing in milliseconds scale?

I know how to get time in seconds via

start=clock()
diff=(clock()-start)/(double) CLOCKS_PER_SEC
cout<<diff

I am using Ubuntu-Linux OS and g++ compiler.

genpfault
  • 51,148
  • 11
  • 85
  • 139
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
  • possible duplicate of [Sub-millisecond precision timing in C or C++](http://stackoverflow.com/questions/2904887/sub-millisecond-precision-timing-in-c-or-c) – phooji Sep 21 '11 at 20:49
  • 1
    It is impressive how many questions ask this same lately using `clock()`. To measure time you should be using `gettimeofday()` or `clock_gettime()`. – Diego Sevilla Sep 21 '11 at 20:50
  • ^ I think there's a newer, better alternative than gettimeofday(), clock_gettime()... mentioned by Ethereal below. I was surprised that gettimeofday() is on the way out, too. – Patrick87 Sep 21 '11 at 20:53
  • Not to be a total sourpuss, but why is this getting upvotes? As pointed out, this question has been asked plenty of times before. – phooji Sep 21 '11 at 21:09

3 Answers3

3

Try diff = (clock() - start) * 1000.0 / CLOCKS_PER_SEC;

The idea is that you multiply the number of clocks by 1000, so that whereas before you might get 2 (seconds), you now get 2000 (milliseconds).

Patrick87
  • 27,682
  • 3
  • 38
  • 73
3

In Linux, take a look at clock_gettime(). It can essentially give you the time elapsed since an arbitrary point, in nanoseconds (which should be good enough for you).

Note that it is specified by the POSIX standard, so you should be fine using it on Unix-derived systems.

kestrel
  • 1,314
  • 10
  • 31
  • +1, specifically, [clock_gettime()](http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html) with `CLOCK_PROCESS_CPUTIME_ID` would be a replacement for `clock()` – Cubbi Sep 21 '11 at 21:14
  • @Jerry Coffin You are correct, of course, but OP did say they are using Linux, which had CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID since 1994, it seems. – Cubbi Sep 21 '11 at 21:25
  • @Cubbi: oops -- I didn't read quite as far down in the question as I should have, and missed the reference to using Linux. My mistake. – Jerry Coffin Sep 21 '11 at 22:18
0

Notes:

On my Dell desktop, which is reasonably quick ... ubuntu bogomips peak at 5210

time(0) takes about 80 nano-seconds (30 million calls in 2.4 seconds)

time(0) allows me to measure clock_gettime() which takes about 1.3 u-seconds per call (2.2 million in 3 seconds)
(I don't remember how many nano-seconds per time step)

So typically, I use the following, with about 3 seconds of invocations.

// ////////////////////////////////////////////////////////////////////////////        
void measuring_something_duration()                        
...            
uint64_t   start_us = dtb::get_system_microsecond();  
do_something_for_about_3_seconds()      
uint64_t test_duration_us = dtb::get_system_microsecond() - start_us;    

uint64_t test_duration_ms = test_duration_us / 1000;
...

which use these functions

// /////////////////////////////////////////////////////////////////////////////   
uint64_t mynamespace::get_system_microsecond(void)  
{  
   uint64_t total_ns = dtb::get_system_nanosecond();   // see below   
   uint64_t ret_val = total_ns / NSPUS;     // NanoSecondsPerMicroSeconds     
   return(ret_val);   
}


// /////////////////////////////////////////////////////////////////////////////  
uint64_t mynamespace::get_system_nanosecond(void)  
{  
   //struct timespec {  __time_t tv_sec;    long int tv_nsec;  };  -- total 8 bytes  
   struct timespec ts;

   // CLOCK_REALTIME - system wide real time clock   
   int status = clock_gettime(CLOCK_REALTIME, &ts);   
   dtb_assert(0 == status);   

   // to 8 byte     from   4 byte   
   uint64_t uli_nsec = ts.tv_nsec;   
   uint64_t uli_sec  = ts.tv_sec;   

   uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second    

   return(total_ns);   
}

Remember to link -lrt

doma
  • 1