libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNTimer.cpp
Go to the documentation of this file.
1 /* Copyright 2009-2014 INSA Lyon, CoReNum
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNTimer.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <CRNUtils/CRNTimer.h>
23 #include <CRNi18n.h>
24 #ifdef _MSC_VER
25 # include <windows.h>
26 #else
27 # include <sys/time.h>
28 #endif
29 using namespace crn;
30 
31 std::map<String, Timer::StopWatch> Timer::stopwatches;
32 
38 double Timer::getTime()
39 {
40 #ifdef _MSC_VER
41  _SYSTEMTIME tim;
42  GetSystemTime(&tim);
43  return ((tim.wDay * 24.0 + tim.wHour) * 60.0 + tim.wMinute) * 60.0 +
44  tim.wSecond + (tim.wMilliseconds / 1000.0); // XXX does not work if the month changes!
45 #else
46  timeval tim;
47  gettimeofday(&tim, nullptr);
48  return double(tim.tv_sec) + (double(tim.tv_usec) / 1000000.0);
49 #endif
50 }
51 
57 void Timer::Start(const String &timername)
58 {
59  StopWatch &sw = stopwatches[timername];
60  sw.stops.clear();
61  sw.t0 = getTime(); // store time as late as possible
62 }
63 
71 double Timer::Split(const String &timername, const String &splitname)
72 {
73  double t = getTime(); // store time as soon as possible
74  double pt;
75  StopWatch &sw = stopwatches[timername];
76  if (sw.stops.size() > 0)
77  pt = sw.stops.back().second;
78  else
79  pt = sw.t0;
80  sw.stops.push_back(std::make_pair(splitname, t));
81  return t - pt;
82 }
83 
90 String Timer::Stats(const String &timername)
91 {
92  StopWatch &sw = stopwatches[timername];
93  if (sw.stops.size() == 0)
94  return _("Unused stopwatch.");
95  int prec = String::Precision();
96  String::Precision() = 6;
97  String s(_("Stopwatch: "));
98  s += timername + U"\n";
99  s += _("Total time: ");
100  double pt = sw.t0;
101  double tot = sw.stops.back().second - pt;
102  s += tot;
103  s += U" s";
104  for (StopTime &st : sw.stops)
105  {
106  s += U"\n" + st.first + U": ";
107  double thistime = st.second - pt;
108  String::Precision() = 6;
109  s += thistime;
110  s += U" s\t(";
111  String::Precision() = 4;
112  s += (thistime * 100.0) / tot;
113  s += U"%)";
114  pt = st.second;
115  }
116  String::Precision() = prec;
117  return s;
118 }
119 
125 void Timer::Destroy(const String &timername)
126 {
127  stopwatches.erase(timername);
128 }
129 
130 static const String CRN_TIMER_UNIQUE_NAME(U"ceci n'est pas un nom");
137 {
138  Start(CRN_TIMER_UNIQUE_NAME);
139 }
140 
146 double Timer::Stop()
147 {
148  double t = Split(CRN_TIMER_UNIQUE_NAME, CRN_TIMER_UNIQUE_NAME);
149  Destroy(CRN_TIMER_UNIQUE_NAME);
150  return t;
151 }
152 
#define _(String)
Definition: CRNi18n.h:51
A UTF32 character string class.
Definition: CRNString.h:61
static int & Precision() noexcept
Precision of the floating point conversion.
Definition: CRNString.cpp:38
static String Stats(const String &timername)
Dumps statistics to a string.
Definition: CRNTimer.cpp:90
static double Split(const String &timername, const String &splitname)
Records time in a stopwatch.
Definition: CRNTimer.cpp:71
static void Destroy(const String &timername)
Frees a stopwatch.
Definition: CRNTimer.cpp:125
static double Stop()
Stops the quick stopwatch.
Definition: CRNTimer.cpp:146
static void Start()
Starts the quick stopwatch.
Definition: CRNTimer.cpp:136