Time measurements

Time measurements.

C++11 supports time measurements, by std::crhono. Remember, that you might need to enable “-std=c++11” flag in your compiler, in order to support C++11 features. Here is a standard example (not developed by me). You can also see examples in Stackoverfow.

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main ()
{
  using namespace std::chrono;

  high_resolution_clock::time_point t1 = high_resolution_clock::now();

  std::cout << "printing out 1000 stars...\n";
  for (int i=0; i<1000; ++i) std::cout << "*";
  std::cout << std::endl;

  high_resolution_clock::time_point t2 = high_resolution_clock::now();

  duration<double> time_span = duration_cast<duration<double>>(t2 - t1);

  std::cout << "It took me " << time_span.count() << " seconds.";
  std::cout << std::endl;

  return 0;
}

In Linux you can use the time command. More.

Gives the time in nanoseconds. In Ubuntu, C++, you need to add -lrt to the list of libraries you link to. Example (in a mainfile):
mm: main.cpp memory_manager.cc
g++ -Wextra -Wall -Wreorder -o mm main.cpp memory_manager.cc -lrt

#include <cstdint> // C++11. Use #include <stdint.h> instead
#include <ctime> 

int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
{
  return (((int64_t)timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
         (((int64_t)timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}

struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);

/* Code to be measured */

clock_gettime(CLOCK_MONOTONIC, &end);
int64_t time;
time = timespecDiff(&end, &start);
std::cout<<"Time: " << time << " ns\n";

If you are interested in seconds, then see below. Thanks to “oogabooga”, a Canadian musician for the code above!

#include <time.h>

  clock_t start, finish, processing_time ;
  start = clock();

  // code to be measured

  finish = clock();

  processing_time = (double(finish-start)/CLOCKS_PER_SEC);

I’ve seen clock() reporting CPU time, instead of real time, so here is another version:

struct timeval start, end;
gettimeofday(&start, NULL);

// code to be measured

gettimeofday(&end, NULL);

delta = ((end.tv_sec  - start.tv_sec) * 1000000u + 
         end.tv_usec - start.tv_usec) / 1.e6;

Nomimal’s animal’s approach

#include <sys/time.h>
#include <time.h>
 
typedef struct timeval wallclock_t;
 
void wallclock_mark(wallclock_t *const tptr)
{
    gettimeofday(tptr, NULL);
}
 
double wallclock_since(wallclock_t *const tptr)
{
    struct timeval  now;
    gettimeofday(&now, NULL);
 
    return difftime(now.tv_sec, tptr->tv_sec)
            + ((double)now.tv_usec - (double)tptr->tv_usec) / 1000000.0;
}

int main(void)
{
    wallclock_t  t;
    double  s;
 
    wallclock_mark(&t);
 
    /*
     * Do something
    */
 
    s = wallclock_since(&t);
    printf("That took %.9f seconds wall clock time.\n", s);
    return 0;
}

or when maximum precision is required use these functions:

#define  _POSIX_C_SOURCE 199309L
#include <time.h>
 
typedef struct timespec wallclock_t;
 
void wallclock_mark(wallclock_t *const tptr)
{
#ifdef CLOCK_MONOTONIC
    clock_gettime(CLOCK_MONOTONIC, tptr);
#else
    clock_gettime(CLOCK_REALTIME, tptr);
#endif
}
 
double wallclock_since(wallclock_t *const tptr)
{
    struct timespec  now;
#ifdef CLOCK_MONOTONIC
    clock_gettime(CLOCK_MONOTONIC, &now);
#else
    clock_gettime(CLOCK_REALTIME, &now);
#endif
    return difftime(now.tv_sec, tptr->tv_sec)
            + ((double)now.tv_nsec - (double)tptr->tv_nsec) / 1000000000.0;
}

int main(void)
{
    wallclock_t  t;
    double  s;
 
    wallclock_mark(&t);
 
    /*
     * Do something
    */
 
    s = wallclock_since(&t);
    printf("That took %.9f seconds wall clock time.\n", s);
    return 0;
}

Have questions about this code? Comments? Did you find a bug? Let me know! 😀
Page created by G. (George) Samaras (DIT)