Stxxl  1.4.0
include/stxxl/bits/common/timer.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/common/timer.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002, 2005 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2007-2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *  Copyright (C) 2008 Johannes Singler <singler@ira.uka.de>
00009  *
00010  *  Distributed under the Boost Software License, Version 1.0.
00011  *  (See accompanying file LICENSE_1_0.txt or copy at
00012  *  http://www.boost.org/LICENSE_1_0.txt)
00013  **************************************************************************/
00014 
00015 #ifndef STXXL_TIMER_HEADER
00016 #define STXXL_TIMER_HEADER
00017 
00018 #ifdef STXXL_BOOST_TIMESTAMP
00019  #include <boost/date_time/posix_time/posix_time.hpp>
00020  #include <cmath>
00021 #else
00022  #include <ctime>
00023  #include <sys/time.h>
00024 #endif
00025 
00026 #include <stxxl/bits/namespace.h>
00027 
00028 
00029 __STXXL_BEGIN_NAMESPACE
00030 
00031 //! \brief Returns number of seconds since the epoch, high resolution.
00032 inline double
00033 timestamp()
00034 {
00035 #ifdef STXXL_BOOST_TIMESTAMP
00036     boost::posix_time::ptime MyTime = boost::posix_time::microsec_clock::local_time();
00037     boost::posix_time::time_duration Duration =
00038         MyTime - boost::posix_time::time_from_string("1970-01-01 00:00:00.000");
00039     double sec = double(Duration.hours()) * 3600. +
00040                  double(Duration.minutes()) * 60. +
00041                  double(Duration.seconds()) +
00042                  double(Duration.fractional_seconds()) / (pow(10., Duration.num_fractional_digits()));
00043     return sec;
00044 #else
00045     struct timeval tp;
00046     gettimeofday(&tp, NULL);
00047     return double(tp.tv_sec) + tp.tv_usec / 1000000.;
00048 #endif
00049 }
00050 
00051 class timer
00052 {
00053     bool running;
00054     double accumulated;
00055     double last_clock;
00056     inline double timestamp();
00057 
00058 public:
00059     inline timer(bool start_immediately = false);
00060     inline void start();
00061     inline void stop();
00062     inline void reset();
00063     inline double seconds();
00064     inline double mseconds();
00065     inline double useconds();
00066 };
00067 
00068 timer::timer(bool start_immediately) : running(false), accumulated(0.)
00069 {
00070     if (start_immediately)
00071         start();
00072 }
00073 
00074 double timer::timestamp()
00075 {
00076     return stxxl::timestamp();
00077 }
00078 
00079 void timer::start()
00080 {
00081     running = true;
00082     last_clock = timestamp();
00083 }
00084 
00085 void timer::stop()
00086 {
00087     running = false;
00088     accumulated += timestamp() - last_clock;
00089 }
00090 
00091 void timer::reset()
00092 {
00093     accumulated = 0.;
00094     last_clock = timestamp();
00095 }
00096 
00097 double timer::mseconds()
00098 {
00099     if (running)
00100         return (accumulated + timestamp() - last_clock) * 1000.;
00101 
00102     return (accumulated * 1000.);
00103 }
00104 
00105 double timer::useconds()
00106 {
00107     if (running)
00108         return (accumulated + timestamp() - last_clock) * 1000000.;
00109 
00110     return (accumulated * 1000000.);
00111 }
00112 
00113 double timer::seconds()
00114 {
00115     if (running)
00116         return (accumulated + timestamp() - last_clock);
00117 
00118     return (accumulated);
00119 }
00120 
00121 __STXXL_END_NAMESPACE
00122 
00123 #endif // !STXXL_TIMER_HEADER
00124 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines