humoto
time.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
5  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
6 
7  @brief
8 */
9 
10 #pragma once
11 
12 
13 namespace humoto
14 {
15  /**
16  * @brief Converts milliseconds to seconds
17  *
18  * @param[in] milliseconds milliseconds
19  *
20  * @return seconds.
21  */
22  inline double HUMOTO_LOCAL convertMillisecondToSecond(const std::size_t milliseconds)
23  {
24  return (milliseconds / 1000.0);
25  }
26 
27 
28  /**
29  * @brief Converts seconds to milliseconds
30  *
31  * @param[in] seconds seconds
32  *
33  * @return milliseconds.
34  */
35  inline std::size_t HUMOTO_LOCAL convertSecondToMillisecond(const double seconds)
36  {
37  return ( static_cast<std::size_t>( round(seconds * 1000) ) );
38  }
39 
40 
41  /**
42  * @brief Converts seconds to milliseconds (with truncation instead of rounding)
43  *
44  * @param[in] seconds seconds
45  *
46  * @return milliseconds.
47  */
48  inline std::size_t HUMOTO_LOCAL truncateSecondToMillisecond(const double seconds)
49  {
50  return ( static_cast<std::size_t>( std::floor(seconds * 1000) ) );
51  }
52 
53 
54  /**
55  * @brief Timer
56  */
58  {
59  public:
60  /**
61  * @brief Constructor. Starts the timer.
62  */
64  {
65  timediff = 0.0;
66  start();
67  }
68 
69 
70  /**
71  * @brief Re/starts the timer.
72  */
73  void start()
74  {
75  gettimeofday(&start_time, 0);
76  }
77 
78 
79  /**
80  * @brief Stops the timer.
81  */
82  double stop()
83  {
84  gettimeofday(&end_time, 0);
85  timediff = (double) end_time.tv_sec - start_time.tv_sec
86  + 0.000001 * (end_time.tv_usec - start_time.tv_usec);
87  return(timediff);
88  }
89 
90 
91  /**
92  * @brief Returns the measured time interval (second).
93  *
94  * @return time interval (second).
95  */
96  double get() const
97  {
98  return(timediff);
99  }
100 
101 
102  /**
103  * @brief Outputs the measured time interval.
104  *
105  * @param[in,out] out output stream.
106  * @param[in] timer the timer.
107  *
108  * @return output stream.
109  */
110  friend std::ostream& operator<< (std::ostream& out, const Timer& timer)
111  {
112  std::ios state(NULL);
113  state.copyfmt(out);
114 
115  out << std::setiosflags(std::ios::fixed) << std::setprecision(6)
116  << "Timer value = " << timer.timediff;
117 
118  out.copyfmt(state);
119  return(out);
120  }
121 
122 
123  private:
124  struct timeval start_time;
125  struct timeval end_time;
126  double timediff;
127  };
128 }
Timer()
Constructor. Starts the timer.
Definition: time.h:63
#define HUMOTO_LOCAL
Definition: export_import.h:26
Timer.
Definition: time.h:57
double timediff
Definition: time.h:126
double HUMOTO_LOCAL convertMillisecondToSecond(const std::size_t milliseconds)
Converts milliseconds to seconds.
Definition: time.h:22
std::size_t HUMOTO_LOCAL convertSecondToMillisecond(const double seconds)
Converts seconds to milliseconds.
Definition: time.h:35
The root namespace of HuMoTo.
Definition: config.h:12
std::size_t HUMOTO_LOCAL truncateSecondToMillisecond(const double seconds)
Converts seconds to milliseconds (with truncation instead of rounding)
Definition: time.h:48
double stop()
Stops the timer.
Definition: time.h:82
void start()
Re/starts the timer.
Definition: time.h:73