utilities/include/simpletimer.h

00001 /********************************************************************************
00002  *  FARSA Utilities Library                                                     *
00003  *  Copyright (C) 2007-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef SIMPLETIMER_H
00021 #define SIMPLETIMER_H
00022 
00023 #include "utilitiesconfig.h"
00024 
00025 #ifdef FARSA_WIN
00026     #include <windows.h>
00027 #else
00028     #include <sys/time.h>
00029 #endif
00030 
00031 namespace farsa {
00032 
00045 class FARSA_UTIL_TEMPLATE SimpleTimer {
00046 public:
00048     SimpleTimer() {
00049 #ifdef FARSA_WIN
00050         QueryPerformanceFrequency( &frequency );
00051         QueryPerformanceCounter( &baseCount );
00052 #else
00053         struct timeval tv;
00054         gettimeofday( &tv, NULL );
00055         lastTime = tv.tv_sec*1000000 + tv.tv_usec;
00056 #endif
00057     };
00059     int tac() {
00060 #ifdef FARSA_WIN
00061         unsigned ticks;
00062         QueryPerformanceCounter( &count );
00063         count.QuadPart -= baseCount.QuadPart;
00064         ticks = unsigned( count.QuadPart * LONGLONG (1000000) / frequency.QuadPart );
00065         return ticks;
00066 #else
00067         struct timeval tv;
00068         gettimeofday( &tv, NULL );
00069         return (tv.tv_sec*1000000 + tv.tv_usec) - lastTime;
00070 #endif
00071     };
00073     int tic() {
00074 #ifdef FARSA_WIN
00075         unsigned ticks;
00076         QueryPerformanceCounter( &count );
00077         count.QuadPart -= baseCount.QuadPart;
00078         ticks = unsigned( count.QuadPart * LONGLONG (1000000) / frequency.QuadPart );
00079         baseCount = count;
00080         return ticks;
00081 #else
00082         struct timeval tv;
00083         gettimeofday( &tv, NULL );
00084         int ret = (tv.tv_sec*1000000 + tv.tv_usec) - lastTime;
00085         lastTime = (tv.tv_sec*1000000 + tv.tv_usec);
00086         return ret;
00087 #endif
00088     };
00089 private:
00090 #ifdef FARSA_WIN
00091     LARGE_INTEGER count;
00092     LARGE_INTEGER frequency;
00093     LARGE_INTEGER baseCount;
00094 #else
00095     long int lastTime;
00096 #endif
00097 };
00098 
00099 } // end namespace farsa
00100 
00101 #endif