Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Forked from kunitoki/mapnik_timer.patch
Created April 5, 2012 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save springmeyer/2311987 to your computer and use it in GitHub Desktop.
Save springmeyer/2311987 to your computer and use it in GitHub Desktop.
boost::chrono in mapnik timer impl
diff --git a/include/mapnik/timer.hpp b/include/mapnik/timer.hpp
index 803f4d0..7247de1 100644
--- a/include/mapnik/timer.hpp
+++ b/include/mapnik/timer.hpp
@@ -29,12 +29,48 @@
#include <sstream>
#include <iomanip>
#include <ctime>
-#ifndef WIN32
-#include <sys/time.h> // for gettimeofday() on unix
+
+#if BOOST_VERSION >= 104700
+ #include <boost/chrono.hpp>
+#else
+ #ifndef WIN32
+ #include <sys/time.h> // for gettimeofday() on unix
+ #include <sys/resource.h>
+ #else
+ #include <windows.h>
+ #endif
#endif
+
namespace mapnik {
+#if BOOST_VERSION >= 104700
+ typedef boost::chrono::system_clock::time_point time_point_t;
+#else
+ typedef double time_point_t;
+#endif
+
+
+inline time_point_t time_now()
+{
+#if BOOST_VERSION >= 104700
+ return boost::chrono::system_clock::now();
+#else
+ #ifndef WIN32
+ struct timeval t;
+ struct timezone tzp;
+ gettimeofday(&t, &tzp);
+ return t.tv_sec + t.tv_usec * 1e-6;
+ #else
+ LARGE_INTEGER t, f;
+ QueryPerformanceCounter(&t);
+ QueryPerformanceFrequency(&f);
+ return double(t.QuadPart) / double(f.QuadPart);
+ #endif
+#endif
+}
+
+
// Measure times in both wall clock time and CPU times. Results are returned in milliseconds.
class timer
{
@@ -46,42 +82,48 @@ public:
void restart()
{
- _stopped = false;
- gettimeofday(&_wall_clock_start, NULL);
- _cpu_start = clock();
+ stopped_ = false;
+ wall_clock_start_ = time_now();
+ cpu_start_ = clock();
}
virtual void stop() const
{
- _stopped = true;
- _cpu_end = clock();
- gettimeofday(&_wall_clock_end, NULL);
+ stopped_ = true;
+ cpu_end_ = clock();
+ wall_clock_end_ = time_now();
}
double cpu_elapsed() const
{
// return elapsed CPU time in ms
- if (!_stopped)
+ if (! stopped_)
+ {
stop();
+ }
- return ((double) (_cpu_end - _cpu_start)) / CLOCKS_PER_SEC * 1000.0;
+ return ((double) (cpu_end_ - cpu_start_)) / CLOCKS_PER_SEC * 1000.0;
}
double wall_clock_elapsed() const
{
// return elapsed wall clock time in ms
- if (!_stopped)
+ if (! stopped_)
+ {
stop();
+ }
- long seconds = _wall_clock_end.tv_sec - _wall_clock_start.tv_sec;
- long useconds = _wall_clock_end.tv_usec - _wall_clock_start.tv_usec;
-
- return ((seconds) * 1000 + useconds / 1000.0) + 0.5;
+#if BOOST_VERSION >= 104700
+ boost::chrono::duration<double, boost::milli> ms = wall_clock_end_ - wall_clock_start_;
+ return ms.count();
+#else
+ return (wall_clock_end_ - wall_clock_start_) * 1000.0;
+#endif
}
protected:
- mutable timeval _wall_clock_start, _wall_clock_end;
- mutable clock_t _cpu_start, _cpu_end;
- mutable bool _stopped;
+ mutable time_point_t wall_clock_start_, wall_clock_end_;
+ mutable clock_t cpu_start_, cpu_end_;
+ mutable bool stopped_;
};
// A progress_timer behaves like a timer except that the destructor displays
@@ -96,7 +138,7 @@ public:
~progress_timer()
{
- if (!_stopped)
+ if (!stopped_)
stop();
}
@@ -116,7 +158,7 @@ public:
}
void discard() {
- _stopped = true;
+ stopped_ = true;
}
private:
@kunitoki
Copy link

kunitoki commented Apr 5, 2012

sure. i posted a wrong old diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment