Stopwatch.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_STOPWATCH_H
8 #define NUKLEI_STOPWATCH_H
9 
10 #include <string>
11 #include <ctime>
12 #include <iostream>
13 #include <ctime>
14 #include <sys/time.h>
15 #include <nuklei/Common.h>
16 
17 namespace nuklei {
18 
19  class Stopwatch
20  {
21  public:
22 
23  typedef enum { QUIET, OCTAVE, TEXT } OutputType;
24 
25  Stopwatch(const std::string &msg) : msg_(msg), outputType_(TEXT)
26  {
27  start_ = clock();
28  lapstart_ = start_;
29 
30  struct timeval time;
31  if (gettimeofday(&time, NULL) != 0)
32  NUKLEI_THROW("Error: gettimeofday.");
33  realStart_ = time.tv_sec * 1000000 + time.tv_usec;
34  realLapStart_ = realStart_;
35  }
36 
37  std::pair<double, double> split(const std::string &spec)
38  {
39  return display(start_, realStart_, spec);
40  }
41 
42  std::pair<double, double> split()
43  {
44  return split("");
45  }
46 
47  std::pair<double, double> lap(const std::string &spec)
48  {
49  std::pair<double, double> t = display(lapstart_, realLapStart_,
50  std::string("lap: ") + spec);
51  lapstart_ = clock();
52  struct timeval time;
53  if (gettimeofday(&time, NULL) != 0)
54  NUKLEI_THROW("Error: gettimeofday.");
55  realLapStart_ = time.tv_sec * 1000000 + time.tv_usec;
56  return t;
57  }
58 
59  std::pair<double, double> lap()
60  {
61  return lap("");
62  }
63 
64  OutputType getOutputType() const
65  {
66  return outputType_;
67  }
68 
69  void setOutputType(OutputType outputType)
70  {
71  outputType_ = outputType;
72  }
73 
74  void reset(const std::string &msg)
75  {
76  msg_ = msg;
77  reset();
78  }
79 
80  void reset()
81  {
82  start_ = clock();
83  lapstart_ = start_;
84 
85  struct timeval time;
86  if (gettimeofday(&time, NULL) != 0)
87  NUKLEI_THROW("Error: gettimeofday.");
88  realStart_ = time.tv_sec * 1000000 + time.tv_usec;
89  realLapStart_ = realStart_;
90  }
91 
92  private:
93  clock_t start_;
94  clock_t lapstart_;
95  unsigned long realStart_;
96  unsigned long realLapStart_;
97  std::string msg_;
98  OutputType outputType_;
99 
100  std::pair<double, double> display(clock_t origin, unsigned long realOrigin,
101  const std::string &spec)
102  {
103  clock_t end = clock();
104  struct timeval time;
105  if (gettimeofday(&time, NULL) != 0)
106  NUKLEI_THROW("Error: gettimeofday.");
107  unsigned long realEnd = time.tv_sec * 1000000 + time.tv_usec;
108 
109  std::string s = " ";
110  std::string tag = "\033[1;31m<Stopwatch>\033[0m ";
111  if (!spec.empty())
112  {
113  s = std::string(" [") + spec + std::string("]: ");
114  tag = "\033[31m<Stopwatch>\033[0m ";
115  }
116  double seconds = ((double) (end - origin)) / CLOCKS_PER_SEC;
117  double realSeconds = double(realEnd - realOrigin) / 1000000;
118 
119  switch (outputType_)
120  {
121  case QUIET: break;
122  case OCTAVE:
123  std::cerr << clean(spec) << " = [ " << clean(spec) << " " <<
124  (double) (end - origin) << " ];\n";
125  break;
126  case TEXT:
127  std::cout << tag
128  << msg_ << s
129  << "cpu=" << seconds << "s "
130  << "real=" << realSeconds
131  << std::endl;
132  break;
133  }
134  return std::make_pair(seconds, realSeconds);
135  }
136 
137  std::string clean(const std::string &dirty)
138  {
139  std::string c = dirty;
140  for (std::string::iterator s_i = c.begin(); s_i != c.end(); s_i++)
141  {
142  if (! (*s_i >= 'a' && *s_i <= 'z') &&
143  ! (*s_i >= 'A' && *s_i <= 'z') &&
144  ! (*s_i >= '0' && *s_i <= '9'))
145  *s_i = '_';
146  }
147  return c;
148  }
149 
150  };
151 
152 }
153 
154 #endif
155 
Public namespace.
Definition: Color.cpp:9
Definition: Stopwatch.h:19
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.