- 3.0.2 core module.
Timer.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <sys/time.h>
9 
10 namespace ct {
11 namespace core {
12 namespace tpl {
13 
15 
18 template <typename SCALAR = double>
19 class Timer
20 {
21 public:
23  Timer() { reset(); }
25 
29  inline void start() { gettimeofday(&start_time, NULL); }
31 
34  inline void stop() { gettimeofday(&stop_time, NULL); }
36 
41  {
42  return (stop_time.tv_sec - start_time.tv_sec) + (stop_time.tv_usec - start_time.tv_usec) * 1e-6;
43  }
44 
46 
49  void reset()
50  {
51  start_time.tv_sec = 0;
52  start_time.tv_usec = 0;
53  stop_time.tv_sec = 0;
54  stop_time.tv_usec = 0;
55  }
56 
57 private:
58  struct timeval start_time;
59  struct timeval stop_time;
60 };
61 }
62 
64 }
65 }
A timer ("stop watch") to record elapsed time based on the system clock.
Definition: Timer.h:19
SCALAR getElapsedTime() const
Get the elapsed time between calls to start() and stop()
Definition: Timer.h:40
Timer()
Default constructor.
Definition: Timer.h:23
void stop()
Trigger stop.
Definition: Timer.h:34
void start()
Trigger start.
Definition: Timer.h:29
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void reset()
Resets the clock.
Definition: Timer.h:49