Stxxl  1.3.2
timer.h
1 /***************************************************************************
2  * include/stxxl/bits/common/timer.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002, 2005 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007-2009 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2008 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_TIMER_HEADER
16 #define STXXL_TIMER_HEADER
17 
18 #ifdef STXXL_BOOST_TIMESTAMP
19  #include <boost/date_time/posix_time/posix_time.hpp>
20  #include <cmath>
21 #else
22  #include <ctime>
23  #include <sys/time.h>
24 #endif
25 
26 #include <stxxl/bits/namespace.h>
27 
28 
29 __STXXL_BEGIN_NAMESPACE
30 
32 inline double
33 timestamp()
34 {
35 #ifdef STXXL_BOOST_TIMESTAMP
36  boost::posix_time::ptime MyTime = boost::posix_time::microsec_clock::local_time();
37  boost::posix_time::time_duration Duration =
38  MyTime - boost::posix_time::time_from_string("1970-01-01 00:00:00.000");
39  double sec = double(Duration.hours()) * 3600. +
40  double(Duration.minutes()) * 60. +
41  double(Duration.seconds()) +
42  double(Duration.fractional_seconds()) / (pow(10., Duration.num_fractional_digits()));
43  return sec;
44 #else
45  struct timeval tp;
46  gettimeofday(&tp, NULL);
47  return double(tp.tv_sec) + tp.tv_usec / 1000000.;
48 #endif
49 }
50 
51 class timer
52 {
53  bool running;
54  double accumulated;
55  double last_clock;
56  inline double timestamp();
57 
58 public:
59  inline timer(bool start_immediately = false);
60  inline void start();
61  inline void stop();
62  inline void reset();
63  inline double seconds();
64  inline double mseconds();
65  inline double useconds();
66 };
67 
68 timer::timer(bool start_immediately) : running(false), accumulated(0.)
69 {
70  if (start_immediately)
71  start();
72 }
73 
74 double timer::timestamp()
75 {
76  return stxxl::timestamp();
77 }
78 
79 void timer::start()
80 {
81  running = true;
82  last_clock = timestamp();
83 }
84 
85 void timer::stop()
86 {
87  running = false;
88  accumulated += timestamp() - last_clock;
89 }
90 
91 void timer::reset()
92 {
93  accumulated = 0.;
94  last_clock = timestamp();
95 }
96 
97 double timer::mseconds()
98 {
99  if (running)
100  return (accumulated + timestamp() - last_clock) * 1000.;
101 
102  return (accumulated * 1000.);
103 }
104 
105 double timer::useconds()
106 {
107  if (running)
108  return (accumulated + timestamp() - last_clock) * 1000000.;
109 
110  return (accumulated * 1000000.);
111 }
112 
113 double timer::seconds()
114 {
115  if (running)
116  return (accumulated + timestamp() - last_clock);
117 
118  return (accumulated);
119 }
120 
121 __STXXL_END_NAMESPACE
122 
123 #endif // !STXXL_TIMER_HEADER
124 // vim: et:ts=4:sw=4