Apache Singa
A General Distributed Deep Learning Library
timer.h
1 
19 #ifndef SINGA_UTILS_TIMER_H
20 #define SINGA_UTILS_TIMER_H
21 
22 #include <chrono>
23 
24 namespace singa {
25 
27 class Timer {
28  public:
29  typedef std::chrono::duration<int> Seconds;
30  typedef std::chrono::duration<int, std::milli> Milliseconds;
31  typedef std::chrono::duration<int, std::ratio<60 * 60>> Hours;
32  typedef std::chrono::duration<int, std::micro> Microseconds;
33 
35  Timer() { Tick(); }
37  void Tick() { last_ = std::chrono::high_resolution_clock::now(); }
41  template <typename T = Milliseconds>
42  int Elapsed() const {
43  static_assert(std::is_same<T, Seconds>::value ||
44  std::is_same<T, Milliseconds>::value ||
45  std::is_same<T, Hours>::value ||
46  std::is_same<T, Microseconds>::value,
47  "Template arg must be Seconds | Milliseconds | Hours | Microseconds");
48  auto now = std::chrono::high_resolution_clock::now();
49  return std::chrono::duration_cast<T>(now - last_).count();
50  }
52  // std::string CurrentTime();
53 
54  private:
55  std::chrono::high_resolution_clock::time_point last_;
56 };
57 }
58 #endif
int Elapsed() const
Return the duration since last call to Tick() or since the creation of Timer.
Definition: timer.h:42
void Tick()
Reset the internal time point to the current time.
Definition: timer.h:37
Timer()
Init the internal time point to the current time.
Definition: timer.h:35
For benchmarking the time cost of operations.
Definition: timer.h:27
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48