ivl 679
ivl/timer.hpp
Go to the documentation of this file.
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00030 #ifndef IVL_TIMER_HPP
00031 #define IVL_TIMER_HPP
00032 
00033 namespace ivl {
00034 
00035 #ifdef _WIN32
00036 // Windows implementation
00037 
00038 #ifndef IVL_MINMAX_MACROS
00039 #ifndef NOMINMAX
00040 #define NOMINMAX
00041 #endif
00042 #endif
00043 
00044 #include <windows.h>
00045 
00046 class timer {
00047 private:
00048     LARGE_INTEGER time_start;
00049     LARGE_INTEGER time_stop;
00050         LONGLONG time_lap;
00051 
00052 protected:
00053         inline void start(void) {
00054         QueryPerformanceCounter(&time_start);
00055     };
00056 
00057     inline void stop(void) {
00058         QueryPerformanceCounter(&time_stop);
00059     };
00060 
00061         inline void mark(void) {
00062                 time_lap = time_stop.QuadPart  - time_start.QuadPart ;
00063     };
00064 
00065         // duration in milliseconds
00066     inline double duration(void)
00067     {
00068         LARGE_INTEGER freq;
00069         QueryPerformanceFrequency(&freq);
00070         double d = (double)(time_stop.QuadPart - time_start.QuadPart)*1000/(double)freq.QuadPart;
00071         return d;
00072     }
00073 
00074         inline double comp_time(void) { return (double) time_lap;}
00075 
00076 public:
00077 
00078         inline void tic(void) { start(); }
00079 
00080         inline double toc(void) { stop(); return duration(); }
00081 
00082         inline double toctic(void) { double t = toc(); tic(); return t; }
00083 };
00084 
00085 #else
00086 // *NIX implementation
00087 
00088 #include <sys/time.h>
00089 
00090 class timer {
00091 private:
00092     struct timeval time_start;
00093     struct timeval time_stop;
00094 
00095 public:
00096 
00097         void tic(void) { gettimeofday(&time_start, 0); }
00098 
00099         double toc(void) {
00100                 struct timeval res;
00101                 gettimeofday(&time_stop, 0);
00102                 timersub(&time_stop, &time_start, &res);
00103                 return double(1000.0 * res.tv_sec + 0.001 * res.tv_usec);
00104         }
00105 
00106         inline double toctic(void) { double t = toc(); tic(); return t; }
00107 };
00108 
00109 #endif // WINDOWS
00110 
00111 } /* namespace ivl */
00112 
00113 #endif // IVL_TIMER_HPP
00114 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations