profiler.h

00001 /**********************************************************************
00002  *
00003  * GEOS - Geometry Engine Open Source
00004  * http://geos.osgeo.org
00005  *
00006  * Copyright (C) 2001-2002 Vivid Solutions Inc.
00007  *
00008  * This is free software; you can redistribute and/or modify it under
00009  * the terms of the GNU Lesser General Public Licence as published
00010  * by the Free Software Foundation. 
00011  * See the COPYING file for more information.
00012  *
00013  **********************************************************************/
00014 
00015 #ifndef GEOS_PROFILER_H
00016 #define GEOS_PROFILER_H
00017 
00018 #include <stdlib.h> 
00019 #include <geos/export.h>
00020 
00021 /* For MingW builds with __STRICT_ANSI__ (-ansi) */
00023 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
00024 /* Allow us to check for presence of gettimeofday in MingW */ 
00025 #include <config.h>
00026 
00027 #include <sys/time.h>
00028 extern "C" {
00029   extern _CRTIMP void __cdecl   _tzset (void);
00030   __MINGW_IMPORT int    _daylight;
00031   __MINGW_IMPORT long   _timezone;
00032   __MINGW_IMPORT char   *_tzname[2];
00033 }
00034 #endif
00035  
00036 #if defined(_MSC_VER) || defined(__MINGW32__) && !defined(HAVE_GETTIMEOFDAY) && !defined(__MINGW64_VERSION_MAJOR)
00037 #include <geos/timeval.h>
00038 #else
00039 #include <sys/time.h>
00040 #endif
00041 
00042 #include <map>
00043 #include <memory>
00044 #include <iostream>
00045 #include <string>
00046 #include <vector>
00047 
00048 #ifndef PROFILE
00049 #define PROFILE 0
00050 #endif
00051 
00052 #ifdef _MSC_VER
00053 #pragma warning(push)
00054 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
00055 #endif
00056 
00057 namespace geos {
00058 namespace util {
00059 
00060 
00061 /*
00062  * \class Profile utils.h geos.h
00063  *
00064  * \brief Profile statistics
00065  */
00066 class GEOS_DLL Profile {
00067 public:
00069         Profile(std::string name);
00070 
00072         ~Profile();
00073 
00075         void start() {
00076                 gettimeofday(&starttime, NULL);
00077         }
00078 
00080         void stop()
00081         {
00082                 gettimeofday(&stoptime, NULL);
00083                 double elapsed = 1000000*(stoptime.tv_sec-starttime.tv_sec)+
00084                         (stoptime.tv_usec-starttime.tv_usec);
00085 
00086                 timings.push_back(elapsed);
00087                 totaltime += elapsed;
00088                 if ( timings.size() == 1 ) max = min = elapsed;
00089                 else
00090                 {
00091                         if ( elapsed > max ) max = elapsed;
00092                         if ( elapsed < min ) min = elapsed;
00093                 }
00094                 avg = totaltime / timings.size();
00095         }
00096 
00098         double getMax() const;
00099 
00101         double getMin() const;
00102 
00104         double getTot() const;
00105 
00107         double getAvg() const;
00108 
00110         size_t getNumTimings() const;
00111 
00113         std::string name;
00114 
00115 
00116 private:
00117 
00118         /* \brief current start and stop times */
00119         struct timeval starttime, stoptime;
00120 
00121         /* \brief actual times */
00122         std::vector<double> timings;
00123 
00124         /* \brief total time */
00125         double totaltime;
00126 
00127         /* \brief max time */
00128         double max;
00129 
00130         /* \brief max time */
00131         double min;
00132 
00133         /* \brief max time */
00134         double avg;
00135 
00136 };
00137 
00138 /*
00139  * \class Profiler utils.h geos.h
00140  *
00141  * \brief Profiling class
00142  *
00143  */
00144 class GEOS_DLL Profiler {
00145 
00146 public:
00147 
00148         Profiler();
00149         ~Profiler();
00150 
00156         static Profiler *instance(void);
00157 
00163         void start(std::string name);
00164 
00170         void stop(std::string name);
00171 
00173         Profile *get(std::string name);
00174 
00175         std::map<std::string, Profile *> profs;
00176 };
00177 
00178 
00180 std::ostream& operator<< (std::ostream& os, const Profile&);
00181 
00183 std::ostream& operator<< (std::ostream& os, const Profiler&);
00184 
00185 } // namespace geos::util
00186 } // namespace geos
00187 
00188 #ifdef _MSC_VER
00189 #pragma warning(pop)
00190 #endif
00191 
00192 #endif // ndef GEOS_PROFILER_H

Generated on 10 Sep 2013 for GEOS by  doxygen 1.4.7