GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
userproject/include/timer.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <chrono>
5 #include <fstream>
6 #include <iostream>
7 #include <string>
8 
9 //------------------------------------------------------------------------
10 // Timer
11 //------------------------------------------------------------------------
13 class Timer
14 {
15 public:
17  Timer(const std::string &message, const std::string &filename = "")
18  : m_Start(std::chrono::high_resolution_clock::now()), m_Message(message), m_Filename(filename)
19  {
20  }
21 
24  {
25  const double duration = get();
26  std::cout << m_Message << duration << " seconds" << std::endl;
27 
28  // If we specified a filename, write duration
29  if(!m_Filename.empty()) {
30  std::ofstream output(m_Filename, std::ios::app);
31  output << duration << std::endl;
32  }
33  }
34 
35  //------------------------------------------------------------------------
36  // Public API
37  //------------------------------------------------------------------------
39  double get() const
40  {
41  auto now = std::chrono::high_resolution_clock::now();
42  std::chrono::duration<double> duration = now - m_Start;
43  return duration.count();
44  }
45 
46 private:
47  //------------------------------------------------------------------------
48  // Members
49  //------------------------------------------------------------------------
50  const std::chrono::time_point<std::chrono::high_resolution_clock> m_Start;
51  const std::string m_Message;
52  const std::string m_Filename;
53 };
54 
55 
56 //------------------------------------------------------------------------
57 // TimerAccumulate
58 //------------------------------------------------------------------------
61 {
62 public:
63  TimerAccumulate(double &accumulator) : m_Start(std::chrono::high_resolution_clock::now()), m_Accumulator(accumulator)
64  {}
65 
67  {
68  m_Accumulator += get();
69  }
70 
71  //------------------------------------------------------------------------
72  // Public API
73  //------------------------------------------------------------------------
75  double get() const
76  {
77  auto now = std::chrono::high_resolution_clock::now();
78  std::chrono::duration<double> duration = now - m_Start;
79  return duration.count();
80  }
81 
82 private:
83  //------------------------------------------------------------------------
84  // Members
85  //------------------------------------------------------------------------
86  std::chrono::time_point<std::chrono::high_resolution_clock> m_Start;
87  double &m_Accumulator;
88 };
~Timer()
Stop the timer and print current elapsed time to terminal.
Definition: userproject/include/timer.h:23
STL namespace.
~TimerAccumulate()
Definition: userproject/include/timer.h:66
TimerAccumulate(double &accumulator)
Definition: userproject/include/timer.h:63
Timer(const std::string &message, const std::string &filename="")
Create a new Timer with the specified name and optionally a filename to append time to...
Definition: userproject/include/timer.h:17
A timer which adds its elapsed time to an accumulator variable on destruction.
Definition: userproject/include/timer.h:60