HepMC3 event record library
Timer.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef BENCHMARK_TIMER_H
7 #define BENCHMARK_TIMER_H
8 
9 #include <iostream>
10 
11 #if defined(_MSC_VER)
12 /**
13  * @class Timer
14  * @brief Used to benchmark MC generators
15  *
16  */
17 class Timer {
18 public:
19  /** Default constructor */
20  Timer(const char* name):m_name(name) { reset(); }
21  void start() {}
22  int elapsed_time() {return 0;}
23  int total_time() {return 0;}
24  void stop() {}
25  void reset() {}
26  void print() { printf("<sys/times.h> header is not present in MS Visual Studio. Dummy implementation of Timer class is used.\n");}
27 private:
28  const char *m_name;
29 
30 };
31 #else
32 #include <sys/times.h>
33 /**
34  * @class Timer
35  * @brief Used to benchmark MC generators
36  *
37  */
38 class Timer {
39 public:
40  /** Default constructor */
41  Timer(const char* name):m_name(name) { reset(); }
42 
43  /** Start or restart the timer */
44  void start() {
45  times(&m_start);
46  }
47 
48  /** Get time elapsed since timer started */
49  int elapsed_time() {
50  times(&m_stop);
51  return 10*(m_stop.tms_utime - m_start.tms_utime + m_stop.tms_stime - m_start.tms_stime);
52  }
53 
54  /** Get total time counted by the timer */
55  int total_time() {
56  return 10*(m_stored.tms_utime + m_stored.tms_stime);
57  }
58 
59  /** Save end time and aggregate build-in clock */
60  void stop() {
61  // Do nothing if timer has not been started
62  if(m_start.tms_utime == 0) return;
63 
64  times(&m_stop);
65 
66  m_stored.tms_utime += m_stop.tms_utime - m_start.tms_utime;
67  m_stored.tms_stime += m_stop.tms_stime - m_start.tms_stime;
68 
69  m_start.tms_utime = 0;
70  m_start.tms_stime = 0;
71  }
72 
73  /** Reset the clock */
74  void reset() {
75  m_start.tms_utime = 0;
76  m_start.tms_stime = 0;
77  m_stored.tms_utime = 0;
78  m_stored.tms_stime = 0;
79  }
80 
81  /** Print time elapsed */
82  void print() {
83  std::cout << m_name << ":" << std::endl;
84  std::cout << " user: " << m_stored.tms_utime*10 << " ms" << std::endl;
85  std::cout << " system: " << m_stored.tms_stime*10 << " ms" << std::endl;
86  }
87 
88 private:
89  const char *m_name; ///< Name of the object
90  struct tms m_start; ///< Start
91  struct tms m_stop; ///< Stop
92  struct tms m_stored; ///< Stored
93 };
94 #endif
95 #endif
const char * m_name
Name of the object.
Definition: Timer.h:89
void start()
Definition: Timer.h:44
Timer(const char *name)
Definition: Timer.h:41
void print()
Definition: Timer.h:82
int elapsed_time()
Definition: Timer.h:49
void stop()
Definition: Timer.h:60
int total_time()
Definition: Timer.h:55
struct tms m_start
Start.
Definition: Timer.h:90
Annotation for function names.
Definition: attr.h:36
Used to benchmark MC generators.
Definition: Timer.h:38
struct tms m_stored
Stored.
Definition: Timer.h:92
struct tms m_stop
Stop.
Definition: Timer.h:91
void reset()
Definition: Timer.h:74