PF_API 0.52

Code/Libs/Utils_LIB/UPrecisionTimer.h

Go to the documentation of this file.
00001 
00002 //    Copyright (C) 2004-2007 Dylan Blair
00003 //
00004 //    email: dblair@alumni.cs.utexas.edu
00005 //
00006 //    This library is free software; you can redistribute it and/or
00007 //    modify it under the terms of the GNU Lesser General Public
00008 //    License as published by the Free Software Foundation; either
00009 //    version 2.1 of the License, or (at your option) any later version.
00010 //
00011 //    This library is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 //    Lesser General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU Lesser General Public
00017 //    License along with this library; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 #ifndef UPRECISION_TIMER_H
00022 #define UPRECISION_TIMER_H
00023 
00024 #ifdef _WIN32
00025 #include <windows.h>
00026 
00027 namespace OpenSkyNet {
00028     namespace Utils {
00029         class PrecisionTimer {
00030             LARGE_INTEGER _freq, _begin, _end, _stamp;
00031         public:
00032             PrecisionTimer() { QueryPerformanceFrequency(&_freq); }
00033 
00034             inline void start() { QueryPerformanceCounter(&_begin); }
00035 
00036             inline double stop() {
00037                 QueryPerformanceCounter(&_end);
00038                 //return seconds passed
00039                 return (static_cast<double>(_end.QuadPart - _begin.QuadPart) / _freq.QuadPart);
00040             }
00041 
00042             inline double stamp() {
00043                 QueryPerformanceCounter(&_stamp);
00044                 //return in seconds
00045                 return (static_cast<double>(_stamp.QuadPart) / _freq.QuadPart);
00046             }
00047         };
00048     }
00049 }
00050 #else
00051 #include <sys/time.h>
00052 
00053 namespace OpenSkyNet {
00054     namespace Utils {
00055         class PrecisionTimer {
00056             timeval _begin, _end, _stamp;
00057         public:
00058             inline void start() { gettimeofday(&_begin, 0); }
00059 
00060             inline double stop() {
00061                 gettimeofday(&_end, 0);
00062                 //return seconds passed
00063                 double t1 = static_cast<double>(_begin.tv_sec + static_cast<double>(_begin.tv_usec)/(1000000));
00064                 double t2 = static_cast<double>(_end.tv_sec + static_cast<double>(_end.tv_usec)/(1000000));
00065                 return t2 - t1;
00066             }
00067 
00068             inline double stamp() {
00069                 gettimeofday(&_stamp, 0);
00070                 //return in seconds
00071                 return static_cast<double>(_stamp.tv_sec + static_cast<double>(_stamp.tv_usec)/(1000000));
00072             }
00073         };
00074     }
00075 }
00076 #endif
00077 
00078 #endif //UPRECISION_TIMER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines