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 <stdlib.h>
19 #include <geos/export.h>
20 
21 /* For MingW builds with __STRICT_ANSI__ (-ansi) */
23 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
24 /* Allow us to check for presence of gettimeofday in MingW */
25 #include <config.h>
26 
27 #include <sys/time.h>
28 extern "C" {
29  extern _CRTIMP void __cdecl _tzset(void);
30  __MINGW_IMPORT int _daylight;
31  __MINGW_IMPORT long _timezone;
32  __MINGW_IMPORT char* _tzname[2];
33 }
34 #endif
35 
36 #if defined(_MSC_VER) || defined(__MINGW32__) && !defined(HAVE_GETTIMEOFDAY) && !defined(__MINGW64_VERSION_MAJOR)
37 #include <geos/timeval.h>
38 #else
39 #include <sys/time.h>
40 #endif
41 
42 #include <map>
43 #include <memory>
44 #include <iostream>
45 #include <string>
46 #include <vector>
47 
48 #ifndef PROFILE
49 #define PROFILE 0
50 #endif
51 
52 #ifdef _MSC_VER
53 #pragma warning(push)
54 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
55 #endif
56 
57 namespace geos {
58 namespace util {
59 
60 
61 /*
62  * \class Profile utils.h geos.h
63  *
64  * \brief Profile statistics
65  */
66 class GEOS_DLL Profile {
67 public:
69  Profile(std::string name);
70 
72  ~Profile();
73 
75  void
76  start()
77  {
78  gettimeofday(&starttime, nullptr);
79  }
80 
82  void
83  stop()
84  {
85  gettimeofday(&stoptime, nullptr);
86  double elapsed = static_cast<double>(
87  1000000 * (stoptime.tv_sec - starttime.tv_sec)
88  + (stoptime.tv_usec - starttime.tv_usec));
89 
90  timings.push_back(elapsed);
91  totaltime += elapsed;
92  if(timings.size() == 1) {
93  max = min = elapsed;
94  }
95  else {
96  if(elapsed > max) {
97  max = elapsed;
98  }
99  if(elapsed < min) {
100  min = elapsed;
101  }
102  }
103  avg = totaltime / static_cast<double>(timings.size());
104  }
105 
107  double getMax() const;
108 
110  double getMin() const;
111 
113  double getTot() const;
114 
116  double getAvg() const;
117 
119  size_t getNumTimings() const;
120 
122  std::string name;
123 
124 
125 private:
126 
127  /* \brief current start and stop times */
128  struct timeval starttime, stoptime;
129 
130  /* \brief actual times */
131  std::vector<double> timings;
132 
133  /* \brief total time */
134  double totaltime;
135 
136  /* \brief max time */
137  double max;
138 
139  /* \brief max time */
140  double min;
141 
142  /* \brief max time */
143  double avg;
144 
145 };
146 
147 /*
148  * \class Profiler utils.h geos.h
149  *
150  * \brief Profiling class
151  *
152  */
153 class GEOS_DLL Profiler {
154 
155 public:
156 
157  Profiler();
158  ~Profiler();
159 
165  static Profiler* instance(void);
166 
172  void start(std::string name);
173 
179  void stop(std::string name);
180 
182  Profile* get(std::string name);
183 
184  std::map<std::string, Profile*> profs;
185 };
186 
187 
189 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profile&);
190 
192 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profiler&);
193 
194 } // namespace geos::util
195 } // namespace geos
196 
197 #ifdef _MSC_VER
198 #pragma warning(pop)
199 #endif
200 
201 #endif // ndef GEOS_PROFILER_H
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25