GEOS  3.13.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 #pragma once
16 
17 #include <geos/export.h>
18 #include <chrono>
19 
20 #include <map>
21 #include <memory>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25 
26 #ifndef PROFILE
27 #define PROFILE 0
28 #endif
29 
30 #ifdef _MSC_VER
31 #pragma warning(push)
32 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
33 #endif
34 
35 namespace geos {
36 namespace util {
37 
38 
39 /*
40  * \class Profile utils.h geos.h
41  *
42  * \brief Profile statistics
43  */
44 class GEOS_DLL Profile {
45 public:
46  using timeunit = std::chrono::microseconds;
47 
49  Profile(std::string name);
50 
52  ~Profile() = default;
53 
55  void
56  start()
57  {
58  starttime = std::chrono::high_resolution_clock::now();
59  }
60 
62  void
63  stop()
64  {
65  stoptime = std::chrono::high_resolution_clock::now();
66  auto elapsed = std::chrono::duration_cast<timeunit>(stoptime - starttime);
67 
68  timings.push_back(elapsed);
69 
70  totaltime += elapsed;
71  if(timings.size() == 1) {
72  max = min = elapsed;
73  }
74  else {
75  if(elapsed > max) {
76  max = elapsed;
77  }
78  if(elapsed < min) {
79  min = elapsed;
80  }
81  }
82 
83  avg = static_cast<double>(totaltime.count()) / static_cast<double>(timings.size());
84  }
85 
87  double getMax() const;
88 
90  double getMin() const;
91 
93  double getTot() const;
94 
96  std::string getTotFormatted() const;
97 
99  double getAvg() const;
100 
102  std::size_t getNumTimings() const;
103 
105  std::string name;
106 
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 
142  Profiler(const Profiler&) = delete;
143  Profiler& operator=(const Profiler&) = delete;
144 
150  static Profiler* instance(void);
151 
157  void start(std::string name);
158 
164  void stop(std::string name);
165 
167  Profile* get(std::string name);
168 
169  std::map<std::string, std::unique_ptr<Profile>> profs;
170 };
171 
172 
174 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profile&);
175 
177 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profiler&);
178 
179 } // namespace geos::util
180 } // namespace geos
181 
182 #ifdef _MSC_VER
183 #pragma warning(pop)
184 #endif
185 
std::ostream & operator<<(std::ostream &os, const Profile &)
Return a string representing the Profile.
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25