Boost logo

Boost Users :

From: François Duranleau (duranlef_at_[hidden])
Date: 2007-04-20 12:20:35


On Fri, 20 Apr 2007, Przemyslaw Tumidajewicz wrote:

> I've been trying to use the boost/timer class to provide some
> information on the time taken by subtasks in my program. I have noticed
> that times seem to be reported fine as long as subtasks are doing CPU
> processing, but disk operations seem to suspend the timer. For instance
> buffering a bigger file to memory is reported to take eg. half a second,
> while it can clearly be seen that the program is running for 15 seconds.
> Any ideas on what can cause this behaviour and if there is any way to
> make it work properly? I am using g++ 3.4.4 with boost 1.33.1 under Win
> XP Pro SP2.

boost::timer uses std::clock to measure time, which measures only the time
a process spends using the CPU. What you want is to use a real time clock.
You can to that with the Boost.DateTime library.

Example: (using microseconds precision, untested)

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

// adapted from <boost/timer.hpp>
class real_timer
{
   public :

     timer() { restart() ; }

     void restart()
     {
         using ::boost::posix_time::microsec_clock ;
         _start_time = microsec_clock::local_time() ;
     }

     double elapsed() const
     {
         using ::boost::posix_time::microsec_clock ;
         return (microsec_clock::local_time() - _start_time)
             .total_microseconds() / 1000000.0 ;
     }

   private :

     ::boost::posix_time::ptime _start_time ;

} ;

-- 
François Duranleau
LIGUM, Université de Montréal

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net