7 #ifndef NUKLEI_STOPWATCH_H
8 #define NUKLEI_STOPWATCH_H
23 typedef enum { QUIET, OCTAVE, TEXT } OutputType;
25 Stopwatch(
const std::string &msg) : msg_(msg), outputType_(TEXT)
31 if (gettimeofday(&time, NULL) != 0)
33 realStart_ = time.tv_sec * 1000000 + time.tv_usec;
34 realLapStart_ = realStart_;
37 std::pair<double, double> split(
const std::string &spec)
39 return display(start_, realStart_, spec);
42 std::pair<double, double> split()
47 std::pair<double, double> lap(
const std::string &spec)
49 std::pair<double, double> t = display(lapstart_, realLapStart_,
50 std::string(
"lap: ") + spec);
53 if (gettimeofday(&time, NULL) != 0)
55 realLapStart_ = time.tv_sec * 1000000 + time.tv_usec;
59 std::pair<double, double> lap()
64 OutputType getOutputType()
const
69 void setOutputType(OutputType outputType)
71 outputType_ = outputType;
74 void reset(
const std::string &msg)
86 if (gettimeofday(&time, NULL) != 0)
88 realStart_ = time.tv_sec * 1000000 + time.tv_usec;
89 realLapStart_ = realStart_;
95 unsigned long realStart_;
96 unsigned long realLapStart_;
98 OutputType outputType_;
100 std::pair<double, double> display(clock_t origin,
unsigned long realOrigin,
101 const std::string &spec)
103 clock_t end = clock();
105 if (gettimeofday(&time, NULL) != 0)
107 unsigned long realEnd = time.tv_sec * 1000000 + time.tv_usec;
110 std::string tag =
"\033[1;31m<Stopwatch>\033[0m ";
113 s = std::string(
" [") + spec + std::string(
"]: ");
114 tag =
"\033[31m<Stopwatch>\033[0m ";
116 double seconds = ((double) (end - origin)) / CLOCKS_PER_SEC;
117 double realSeconds = double(realEnd - realOrigin) / 1000000;
123 std::cerr << clean(spec) <<
" = [ " << clean(spec) <<
" " <<
124 (double) (end - origin) <<
" ];\n";
129 <<
"cpu=" << seconds <<
"s "
130 <<
"real=" << realSeconds
134 return std::make_pair(seconds, realSeconds);
137 std::string clean(
const std::string &dirty)
139 std::string c = dirty;
140 for (std::string::iterator s_i = c.begin(); s_i != c.end(); s_i++)
142 if (! (*s_i >=
'a' && *s_i <=
'z') &&
143 ! (*s_i >=
'A' && *s_i <=
'z') &&
144 ! (*s_i >=
'0' && *s_i <=
'9'))