00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef STXXL_TIMER_HEADER
00014 #define STXXL_TIMER_HEADER
00015
00016 #ifdef STXXL_BOOST_TIMESTAMP
00017 #include <boost/date_time/posix_time/posix_time.hpp>
00018 #include <cmath>
00019 #else
00020 #include <ctime>
00021 #include <sys/time.h>
00022 #endif
00023
00024 #include <stxxl/bits/namespace.h>
00025
00026
00027 __STXXL_BEGIN_NAMESPACE
00028
00030 inline double
00031 timestamp()
00032 {
00033 #ifdef STXXL_BOOST_TIMESTAMP
00034 boost::posix_time::ptime MyTime = boost::posix_time::microsec_clock::local_time();
00035 boost::posix_time::time_duration Duration =
00036 MyTime - boost::posix_time::time_from_string("1970-01-01 00:00:00.000");
00037 double sec = double(Duration.hours()) * 3600. +
00038 double(Duration.minutes()) * 60. +
00039 double(Duration.seconds()) +
00040 double(Duration.fractional_seconds()) / (pow(10., Duration.num_fractional_digits()));
00041 return sec;
00042 #else
00043 struct timeval tp;
00044 gettimeofday(&tp, NULL);
00045 return double(tp.tv_sec) + tp.tv_usec / 1000000.;
00046 #endif
00047 }
00048
00049 class timer
00050 {
00051 bool running;
00052 double accumulated;
00053 double last_clock;
00054 inline double timestamp();
00055
00056 public:
00057 inline timer(bool start_immediately = false);
00058 inline void start();
00059 inline void stop();
00060 inline void reset();
00061 inline double seconds();
00062 inline double mseconds();
00063 inline double useconds();
00064 };
00065
00066 timer::timer(bool start_immediately) : running(false), accumulated(0.)
00067 {
00068 if (start_immediately)
00069 start();
00070 }
00071
00072 double timer::timestamp()
00073 {
00074 return stxxl::timestamp();
00075 }
00076
00077 void timer::start()
00078 {
00079 running = true;
00080 last_clock = timestamp();
00081 }
00082
00083 void timer::stop()
00084 {
00085 running = false;
00086 accumulated += timestamp() - last_clock;
00087 }
00088
00089 void timer::reset()
00090 {
00091 accumulated = 0.;
00092 last_clock = timestamp();
00093 }
00094
00095 double timer::mseconds()
00096 {
00097 if (running)
00098 return (accumulated + timestamp() - last_clock) * 1000.;
00099
00100 return (accumulated * 1000.);
00101 }
00102
00103 double timer::useconds()
00104 {
00105 if (running)
00106 return (accumulated + timestamp() - last_clock) * 1000000.;
00107
00108 return (accumulated * 1000000.);
00109 }
00110
00111 double timer::seconds()
00112 {
00113 if (running)
00114 return (accumulated + timestamp() - last_clock);
00115
00116 return (accumulated);
00117 }
00118
00119 __STXXL_END_NAMESPACE
00120
00121 #endif // !STXXL_TIMER_HEADER
00122