# profiler.h

    00001 /**********************************************************************
    00002  * $Id: profiler.h 2021 2007-09-11 02:02:44Z csavage $
    00003  *
    00004  * GEOS - Geometry Engine Open Source
    00005  * http://geos.refractions.net
    00006  *
    00007  * Copyright (C) 2001-2002 Vivid Solutions Inc.
    00008  *
    00009  * This is free software; you can redistribute and/or modify it under
    00010  * the terms of the GNU Lesser General Public Licence as published
    00011  * by the Free Software Foundation.
    00012  * See the COPYING file for more information.
    00013  *
    00014  **********************************************************************/
    00015
    00016 #ifndef GEOS_PROFILER_H
    00017 #define GEOS_PROFILER_H
    00018
    00019 /* For MingW builds with __STRICT_ANSI__ (-ansi) */
    00020 #if defined(__MINGW32__)
    00021 /* Allow us to check for presence of gettimeofday in MingW */
    00022 #include <config.h>
    00023
    00024 #include <sys/time.h>
    00025 extern "C" {
    00026   extern _CRTIMP void __cdecl   _tzset (void);
    00027   __MINGW_IMPORT int    _daylight;
    00028   __MINGW_IMPORT long   _timezone;
    00029   __MINGW_IMPORT char   *_tzname[2];
    00030 }
    00031 #endif
    00032
    00033 #if defined(_MSC_VER) || defined(__MINGW32__) && !defined(HAVE_GETTIMEOFDAY)
    00034 #include <geos/timeval.h>
    00035 #else
    00036 #include <sys/time.h>
    00037 #endif
    00038
    00039 #include <map>
    00040 #include <memory>
    00041 #include <iostream>
    00042 #include <string>
    00043 #include <vector>
    00044
    00045 #ifndef PROFILE
    00046 #define PROFILE 0
    00047 #endif
    00048
    00049 namespace geos {
    00050 namespace util {
    00051
    00052
    00053 /*
    00054  * \class Profile utils.h geos.h
    00055  *
    00056  * \brief Profile statistics
    00057  */
    00058 class Profile {
    00059 public:
    00061         Profile(std::string name);
    00062
    00064         ~Profile();
    00065
    00067         void start() {
    00068                 gettimeofday(&starttime, NULL);
    00069         }
    00070
    00072         void stop()
    00073         {
    00074                 gettimeofday(&stoptime, NULL);
    00075                 double elapsed = 1000000*(stoptime.tv_sec-starttime.tv_sec)+
    00076                         (stoptime.tv_usec-starttime.tv_usec);
    00077
    00078                 timings.push_back(elapsed);
    00079                 totaltime += elapsed;
    00080                 if ( timings.size() == 1 ) max = min = elapsed;
    00081                 else
    00082                 {
    00083                         if ( elapsed > max ) max = elapsed;
    00084                         if ( elapsed < min ) min = elapsed;
    00085                 }
    00086                 avg = totaltime / timings.size();
    00087         }
    00088
    00090         double getMax() const;
    00091
    00093         double getMin() const;
    00094
    00096         double getTot() const;
    00097
    00099         double getAvg() const;
    00100
    00102         size_t getNumTimings() const;
    00103
    00105         std::string name;
    00106
    00107
    00108 private:
    00109
    00110         /* \brief current start and stop times */
    00111         struct timeval starttime, stoptime;
    00112
    00113         /* \brief actual times */
    00114         std::vector<double> timings;
    00115
    00116         /* \brief total time */
    00117         double totaltime;
    00118
    00119         /* \brief max time */
    00120         double max;
    00121
    00122         /* \brief max time */
    00123         double min;
    00124
    00125         /* \brief max time */
    00126         double avg;
    00127
    00128 };
    00129
    00130 /*
    00131  * \class Profiler utils.h geos.h
    00132  *
    00133  * \brief Profiling class
    00134  *
    00135  */
    00136 class Profiler {
    00137
    00138 public:
    00139
    00140         Profiler();
    00141         ~Profiler();
    00142
    00148         static Profiler *instance(void);
    00149
    00155         void start(std::string name);
    00156
    00162         void stop(std::string name);
    00163
    00165         Profile *get(std::string name);
    00166
    00167         std::map<std::string, Profile *> profs;
    00168 };
    00169
    00170
    00172 std::ostream& operator<< (std::ostream& os, const Profile&);
    00173
    00175 std::ostream& operator<< (std::ostream& os, const Profiler&);
    00176
    00177 } // namespace geos::util
    00178 } // namespace geos
    00179
    00180 #endif // ndef GEOS_PROFILER_H
    00181
    00182 /**********************************************************************
    00183  * $Log$
    00184  * Revision 1.8  2006/06/12 11:29:23  strk
    00185  * unsigned int => size_t
    00186  *
    00187  * Revision 1.7  2006/03/06 19:40:47  strk
    00188  * geos::util namespace. New GeometryCollection::iterator interface, many cleanups.
    00189  *
    00190  * Revision 1.6  2006/03/03 10:46:21  strk
    00191  * Removed 'using namespace' from headers, added missing headers in .cpp files, removed useless includes in headers (bug#46)
    00192  *
    00193  * Revision 1.5  2005/02/01 14:18:04  strk
    00194  * Made profiler start/stop inline
    00195  *
    00196  * Revision 1.4  2004/12/03 16:21:07  frank
    00197  * dont try for sys/time.h with MSVC
    00198  *
    00199  * Revision 1.3  2004/11/30 16:44:16  strk
    00200  * Added gettimeofday implementation for win32, curtesy of Wu Yongwei.
    00201  *
    00202  * Revision 1.2  2004/11/04 08:49:13  strk
    00203  * Unlinked new documentation.
    00204  *
    00205  * Revision 1.1  2004/11/01 16:43:04  strk
    00206  * Added Profiler code.
    00207  * Temporarly patched a bug in DoubleBits (must check drawbacks).
    00208  * Various cleanups and speedups.
    00209  *
    00210  **********************************************************************/