Jannat Arora
Jannat Arora

Reputation: 2989

Recording time taken by code

I used the following function to find the time taken by my code.

  #include <sys/time.h>
  struct timeval start, end;
  gettimeofday(&start,NULL);   
  //mycode   
  gettimeofday(&end,NULL);
  cout<<" time taken by my code: "<<((end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec ) / 1000.0<<" msec"<<endl;

I observed that even though my code runs for 2 hours, yet the time reported by the above function is 1213 milliseconds. I am not able to understand as to why is it happened. Also is there a way by which I may record the time taken by my code in hours correctly

Upvotes: 0

Views: 212

Answers (4)

aakash
aakash

Reputation: 761

If you are on linux and the code that you want to time is largely the program itself, then you can time your program by passing it as an argument to the time command and look at the 'elapsed time' row.

/usr/bin/time -v <your program's executable>

For example:

/usr/bin/time -v sleep 3                                                                                         .../home/aakashah/workspace/head/src/GroverStorageCommon
        Command being timed: "sleep 3"
        User time (seconds): 0.00
        System time (seconds): 0.00
        Percent of CPU this job got: 0%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.00
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 2176
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 165
        Voluntary context switches: 2
        Involuntary context switches: 0
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

Upvotes: 0

Sagar Kar
Sagar Kar

Reputation: 177

This part of the bench marking process may help your purpose:

#include<time.h>
#include<cstdlib>
...
...
float begin = (float)clock()/CLOCKS_PER_SEC;
...
//do your bench-marking stuffs
...
float end = (float)clock()/CLOCKS_PER_SEC;
float totalTime = end - begin;
cout<<"Time Req in the stuffs: "<<totalTime<<endl;

NOTE: This process is a simple alternative to the chrono library

Upvotes: 0

user4992621
user4992621

Reputation:

On which platform are you doing this? If it's Linux/Unix-like your easiest non-intrusive bet is simply using the time command from the command-line. Is the code you're running single-threaded or not? Some of the functions in time.h (like clock() e.g. ) return the number of ticks against each core, which may or may not be what you want. And the newer stuff in the chrono may not be as exact as you like (a while back I tried to measure time intervals in nanoseconds with chrono, but the lowest time interval I got back back then was 300ns, which was much less exact than I'd hoped).

Upvotes: 1

Howard Hinnant
Howard Hinnant

Reputation: 218750

My best guess is that time_t (the type of tv_sec) on your system is signed 32 bits and that (end.tv_sec - start.tv_sec) * 1000000 overflows.

You could test that theory by making sure that you don't use 32 bit arithmetic for this computation:

(end.tv_sec - start.tv_sec) * 1000000LL

That being said, I advise use of the C++11 <chrono> library instead:

 #include <chrono>

  auto t0 = std::chrono::system_clock::now();
  //mycode
  auto t1 = std::chrono::system_clock::now();
  using milliseconds = std::chrono::duration<double, std::milli>;
  milliseconds ms = t1 - t0;
  std::cout << " time taken by my code: " << ms.count() << '\n';

The <chrono> library has an invariant that none of the "predefined" durations will overflow in less than +/- 292 years. In practice, only nanoseconds will overflow that quickly, and the other durations will have a much larger range. Each duration has static ::min() and ::max() functions you can use to query the range for each.

The original proposal for <chrono> has a decent tutorial section that might be a helpful introduction. It is only slightly dated. What it calls monotonic_clock is now called steady_clock. I believe that is the only significant update it lacks.

Upvotes: 7

Related Questions