Measuring time with boost library

On unix systems, we can use boost Posix Time library. Here’s an example:

#include <iostream>
#include "boost/date_time/posix_time/posix_time.hpp" 

typedef boost::posix_time::ptime Time;
typedef boost::posix_time::time_duration TimeDuration;

int main (){

    Time t1(boost::posix_time::microsec_clock::local_time());

    Time t2(boost::posix_time::microsec_clock::local_time());

    TimeDuration dt = t2 - t1;

    //print formatted date
    std::cout << dt << std::endl; 

    //number of elapsed miliseconds
    long msec = dt.total_milliseconds();

    //print elapsed seconds (with millisecond precision)
    std::cout << msec/1000.0 << std::endl; 

    return 0;

2 thoughts on “Measuring time with boost library”

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s