SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
cpu_timer.cpp
Go to the documentation of this file.
1 /*
2 SeComLib
3 Copyright 2012-2013 TU Delft, Information Security & Privacy Lab (http://isplab.tudelft.nl/)
4 
5 Contributors:
6 Inald Lagendijk (R.L.Lagendijk@TUDelft.nl)
7 Mihai Todor (todormihai@gmail.com)
8 Thijs Veugen (P.J.M.Veugen@tudelft.nl)
9 Zekeriya Erkin (z.erkin@tudelft.nl)
10 
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14 
15 http://www.apache.org/licenses/LICENSE-2.0
16 
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
30 #include "cpu_timer.h"
31 
32 namespace SeComLib {
33 namespace Utils {
38  this->timer.start();
39  }
40 
43  void CpuTimer::Stop () {
44  this->timer.stop();
45  }
46 
49  void CpuTimer::Reset () {
50  this->timer = boost::timer::cpu_timer();
51  }
52 
57  return this->timer.elapsed().user;
58  }
59 
63  std::string CpuTimer::ToString () const {
64  return CpuTimer::ToString(this->GetDuration());
65  }
66 
72  std::stringstream output;
73 
74  int_least64_t userTimeMilliseconds = static_cast<int_least64_t>(userTime / 1000000);
75 
76  int_least64_t miliseconds = userTimeMilliseconds % 1000;
77  int_least64_t seconds = userTimeMilliseconds / 1000;
78  int_least64_t hours = seconds / 3600;
79  seconds = seconds % 3600;
80  int_least64_t minutes = seconds / 60;
81  seconds = seconds % 60;
82 
84  if (hours < 10) output << "0" << hours;
85  else output << hours;
86  output << ":";
87  if (minutes < 10) output << "0" << minutes;
88  else output << minutes;
89  output << ":";
90  if (seconds < 10) output << "0" << seconds;
91  else output << seconds;
92  output << ".";
93  if (miliseconds < 10) output << "00" << miliseconds;
94  else if (miliseconds < 100) output << "0" << miliseconds;
95  else output << miliseconds;
96 
97  return output.str();
98  }
99 
100 }//namespace Utils
101 }//namespace SeComLib
boost::timer::nanosecond_type NanosecondType
Nanosecond data type (int_least64_t)
Definition: cpu_timer.h:50
std::string ToString() const
Returns the elapsed user process time as a formatted string (HH::MM::SS.mmm)
Definition: cpu_timer.cpp:63
void Stop()
Stops the timer.
Definition: cpu_timer.cpp:43
Definition of class CpuTimer.
boost::timer::cpu_timer timer
the internal timer variable
Definition: cpu_timer.h:75
CpuTimer()
Default constructor.
Definition: cpu_timer.cpp:37
NanosecondType GetDuration() const
Returns the elapsed user process time (in nanoseconds)
Definition: cpu_timer.cpp:56
void Reset()
Resets the timer.
Definition: cpu_timer.cpp:49