GEOS  3.8.0dev
profiler.h
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  */
45 class GEOS_DLL Profile {
46 public:
47  using timeunit = std::chrono::microseconds;
48 
50  Profile(std::string name);
51 
53  ~Profile() = default;
54 
56  void
57  start()
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 private:
110  /* \brief current start and stop times */
111  std::chrono::high_resolution_clock::time_point starttime, stoptime;
112 
113  /* \brief actual times */
114  std::vector<timeunit> timings;
115 
116  /* \brief total time */
117  timeunit totaltime;
118 
119  /* \brief max time */
120  timeunit max;
121 
122  /* \brief max time */
123  timeunit min;
124 
125  /* \brief avg time */
126  double avg;
127 };
128 
129 /*
130  * \class Profiler utils.h geos.h
131  *
132  * \brief Profiling class
133  *
134  */
135 class GEOS_DLL Profiler {
136 
137 public:
138 
139  Profiler() = default;
140  ~Profiler() = default;
141 
147  static Profiler* instance(void);
148 
154  void start(std::string name);
155 
161  void stop(std::string name);
162 
164  Profile* get(std::string name);
165 
166  std::map<std::string, std::unique_ptr<Profile>> profs;
167 };
168 
169 
171 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profile&);
172 
174 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profiler&);
175 
176 } // namespace geos::util
177 } // namespace geos
178 
179 #ifdef _MSC_VER
180 #pragma warning(pop)
181 #endif
182 
183 #endif // ndef GEOS_PROFILER_H
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25