GEOS  3.9.1dev
profiler.h
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2001-2002 Vivid Solutions Inc.
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU Lesser General Public Licence as published
10  * by the Free Software Foundation.
11  * See the COPYING file for more information.
12  *
13  **********************************************************************/
14 
15 #ifndef GEOS_PROFILER_H
16 #define GEOS_PROFILER_H
17 
18 #include <geos/export.h>
19 #include <chrono>
20 
21 #include <map>
22 #include <memory>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 
27 #ifndef PROFILE
28 #define PROFILE 0
29 #endif
30 
31 #ifdef _MSC_VER
32 #pragma warning(push)
33 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
34 #endif
35 
36 namespace geos {
37 namespace util {
38 
39 
40 /*
41  * \class Profile utils.h geos.h
42  *
43  * \brief Profile statistics
44  */
46 public:
47  using timeunit = std::chrono::microseconds;
48 
50  Profile(std::string name);
51 
53  ~Profile() = default;
54 
56  void
58  {
59  starttime = std::chrono::high_resolution_clock::now();
60  }
61 
63  void
64  stop()
65  {
66  stoptime = std::chrono::high_resolution_clock::now();
67  auto elapsed = std::chrono::duration_cast<timeunit>(stoptime - starttime);
68 
69  timings.push_back(elapsed);
70 
71  totaltime += elapsed;
72  if(timings.size() == 1) {
73  max = min = elapsed;
74  }
75  else {
76  if(elapsed > max) {
77  max = elapsed;
78  }
79  if(elapsed < min) {
80  min = elapsed;
81  }
82  }
83 
84  avg = static_cast<double>(totaltime.count()) / static_cast<double>(timings.size());
85  }
86 
88  double getMax() const;
89 
91  double getMin() const;
92 
94  double getTot() const;
95 
97  std::string getTotFormatted() const;
98 
100  double getAvg() const;
101 
103  size_t getNumTimings() const;
104 
106  std::string name;
107 
108 
109 
110 private:
111  /* \brief current start and stop times */
112  std::chrono::high_resolution_clock::time_point starttime, stoptime;
113 
114  /* \brief actual times */
115  std::vector<timeunit> timings;
116 
117  /* \brief total time */
119 
120  /* \brief max time */
122 
123  /* \brief max time */
125 
126  /* \brief avg time */
127  double avg;
128 };
129 
130 /*
131  * \class Profiler utils.h geos.h
132  *
133  * \brief Profiling class
134  *
135  */
137 
138 public:
139 
140  Profiler() = default;
141  ~Profiler() = default;
142 
143  Profiler(const Profiler&) = delete;
144  Profiler& operator=(const Profiler&) = delete;
145 
151  static Profiler* instance(void);
152 
158  void start(std::string name);
159 
165  void stop(std::string name);
166 
168  Profile* get(std::string name);
169 
170  std::map<std::string, std::unique_ptr<Profile>> profs;
171 };
172 
173 
175 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profile&);
176 
178 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profiler&);
179 
180 } // namespace geos::util
181 } // namespace geos
182 
183 #ifdef _MSC_VER
184 #pragma warning(pop)
185 #endif
186 
187 #endif // ndef GEOS_PROFILER_H
std::chrono::high_resolution_clock::time_point stoptime
Definition: profiler.h:112
#define GEOS_DLL
Definition: export.h:28
std::map< std::string, std::unique_ptr< Profile > > profs
Definition: profiler.h:170
std::string name
Profile name.
Definition: profiler.h:106
void stop()
stop current timer
Definition: profiler.h:64
std::chrono::microseconds timeunit
Definition: profiler.h:47
void start()
start a new timer
Definition: profiler.h:57
timeunit totaltime
Definition: profiler.h:118
Basic namespace for all GEOS functionalities.
std::vector< timeunit > timings
Definition: profiler.h:115
std::ostream & operator<<(std::ostream &os, const Profile &)
Return a string representing the Profile.