0

I am having some problem by reading out the system time under Ubuntu. I am trying to get the difference of two ptime variables.

This is my declaration:

#include "boost/date_time/posix_time/posix_time.hpp"

boost::posix_time::ptime now  = boost::posix_time::microsec_clock::universal_time();
boost::posix_time::ptime last_time = now;
boost::posix_time::time_duration dt;
...

After a time I update the now variable and build the difference

now  = boost::posix_time::second_clock::universal_time();
dt = last_time - now;

The problem is that I want to work in millisecond resolution in my project so I divide the time I get by 1000 (after converting the time to microseconds like below).

printf("%f", (double)dt.total_microseconds());

The problem is that I only got values in second resolution. I already tried local_time() instead of universal_time(). It did not solved my problem...

Does any of you have any suggestions?

Thank you for the help.

bdvd
  • 117
  • 1
  • 1
  • 11

2 Answers2

2

things could be done pretty easy in C++11.

#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::milliseconds;
using std::chrono::nanoseconds;

auto t0 = high_resolution_clock::now();

// do something here ...

auto t1 = high_resolution_clock::now();
// get miliseconds result.
milliseconds total_milliseconds = std::chrono::duration_cast<milliseconds>(t1 - t0);
// get nanoseconds result.
nanoseconds total_nanoseconds = std::chrono::duration_cast<nanoseconds>(t1 - t0);
qqibrow
  • 2,674
  • 1
  • 21
  • 37
  • Of course when using chrono it's good to keep the code generic and avoid choosing a fixed duration type as long as possible. Just use `auto dt = t1 - t0;` instead of a `duration_cast`. – bames53 Jan 15 '15 at 21:07
  • @bames53 with `auto`, how are you going to specify the milliseconds then? – gsamaras Jan 15 '15 at 21:08
  • @G.Samaras the point is to not specify milliseconds, and to instead simply work with the native duration type. [examples](http://stackoverflow.com/questions/14785533/handling-an-update-loop-using-c-chrono/15839862#15839862) – bames53 Jan 15 '15 at 21:10
  • You got my +1. Thanks @bames53 – gsamaras Jan 15 '15 at 21:12
1

For the C++11 way, check the answer of bames53.


This 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";

Then convert to ms.

I go this from my example here.


Another interesting approach, that might be a bit off for what you want is this:

#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
    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) + 0.5;

    printf("Elapsed time: %ld milliseconds\n", mtime);

    return 0;
}

Source

Community
  • 1
  • 1
gsamaras
  • 66,800
  • 33
  • 152
  • 256
  • You are welcome @bdvd! Make sure you check the new link I just added, about the C++11 way of doing things! – gsamaras Jan 15 '15 at 21:15