GEOS  3.7.1
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 start() {
76  gettimeofday(&starttime, nullptr);
77  }
78 
80  void stop()
81  {
82  gettimeofday(&stoptime, nullptr);
83  double elapsed = static_cast<double>(
84  1000000 * (stoptime.tv_sec - starttime.tv_sec)
85  + (stoptime.tv_usec - starttime.tv_usec));
86 
87  timings.push_back(elapsed);
88  totaltime += elapsed;
89  if ( timings.size() == 1 ) max = min = elapsed;
90  else
91  {
92  if ( elapsed > max ) max = elapsed;
93  if ( elapsed < min ) min = elapsed;
94  }
95  avg = totaltime / static_cast<double>(timings.size());
96  }
97 
99  double getMax() const;
100 
102  double getMin() const;
103 
105  double getTot() const;
106 
108  double getAvg() const;
109 
111  size_t getNumTimings() const;
112 
114  std::string name;
115 
116 
117 private:
118 
119  /* \brief current start and stop times */
120  struct timeval starttime, stoptime;
121 
122  /* \brief actual times */
123  std::vector<double> timings;
124 
125  /* \brief total time */
126  double totaltime;
127 
128  /* \brief max time */
129  double max;
130 
131  /* \brief max time */
132  double min;
133 
134  /* \brief max time */
135  double avg;
136 
137 };
138 
139 /*
140  * \class Profiler utils.h geos.h
141  *
142  * \brief Profiling class
143  *
144  */
145 class GEOS_DLL Profiler {
146 
147 public:
148 
149  Profiler();
150  ~Profiler();
151 
157  static Profiler *instance(void);
158 
164  void start(std::string name);
165 
171  void stop(std::string name);
172 
174  Profile *get(std::string name);
175 
176  std::map<std::string, Profile *> profs;
177 };
178 
179 
181 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profile&);
182 
184 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profiler&);
185 
186 } // namespace geos::util
187 } // namespace geos
188 
189 #ifdef _MSC_VER
190 #pragma warning(pop)
191 #endif
192 
193 #endif // ndef GEOS_PROFILER_H
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25