Stxxl 1.2.1
|
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 * 00008 * Distributed under the Boost Software License, Version 1.0. 00009 * (See accompanying file LICENSE_1_0.txt or copy at 00010 * http://www.boost.org/LICENSE_1_0.txt) 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(); 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() : running(false), accumulated(0.) 00067 { } 00068 00069 double timer::timestamp() 00070 { 00071 return stxxl::timestamp(); 00072 } 00073 00074 void timer::start() 00075 { 00076 running = true; 00077 last_clock = timestamp(); 00078 } 00079 00080 void timer::stop() 00081 { 00082 running = false; 00083 accumulated += timestamp() - last_clock; 00084 } 00085 00086 void timer::reset() 00087 { 00088 accumulated = 0.; 00089 last_clock = timestamp(); 00090 } 00091 00092 double timer::mseconds() 00093 { 00094 if (running) 00095 return (accumulated + timestamp() - last_clock) * 1000.; 00096 00097 return (accumulated * 1000.); 00098 } 00099 00100 double timer::useconds() 00101 { 00102 if (running) 00103 return (accumulated + timestamp() - last_clock) * 1000000.; 00104 00105 return (accumulated * 1000000.); 00106 } 00107 00108 double timer::seconds() 00109 { 00110 if (running) 00111 return (accumulated + timestamp() - last_clock); 00112 00113 return (accumulated); 00114 } 00115 00116 __STXXL_END_NAMESPACE 00117 00118 #endif // !STXXL_TIMER_HEADER