crocoddyl  1.8.1
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
timer.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2020, University of Edinburgh, LAAS-CNRS
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_UTILS_TIMER_H_
10 #define CROCODDYL_CORE_UTILS_TIMER_H_
11 
12 #include <ctime>
13 
14 namespace crocoddyl {
15 
16 class Timer {
17  public:
18  Timer() { clock_gettime(CLOCK_MONOTONIC, &start_); }
19 
20  inline void reset() { clock_gettime(CLOCK_MONOTONIC, &start_); }
21 
22  inline double get_duration() {
23  clock_gettime(CLOCK_MONOTONIC, &finish_);
24  duration_ = static_cast<double>(finish_.tv_sec - start_.tv_sec) * 1000000;
25  duration_ += static_cast<double>(finish_.tv_nsec - start_.tv_nsec) / 1000;
26  return duration_ / 1000.;
27  }
28 
29  inline double get_us_duration() {
30  clock_gettime(CLOCK_MONOTONIC, &finish_);
31  duration_ = static_cast<double>(finish_.tv_sec - start_.tv_sec) * 1000000;
32  duration_ += static_cast<double>(finish_.tv_nsec - start_.tv_nsec) / 1000;
33  return duration_;
34  }
35 
36  private:
37  struct timespec start_;
38  struct timespec finish_;
39  double duration_;
40 };
41 } // namespace crocoddyl
42 
43 #endif // CROCODDYL_CORE_UTILS_TIMER_H_