simpletimer.h
1 /********************************************************************************
2  * FARSA Utilities Library *
3  * Copyright (C) 2007-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef SIMPLETIMER_H
21 #define SIMPLETIMER_H
22 
23 #include "utilitiesconfig.h"
24 
25 #ifdef FARSA_WIN
26  #include <windows.h>
27 #else
28  #include <sys/time.h>
29 #endif
30 
31 namespace farsa {
32 
45 class FARSA_UTIL_TEMPLATE SimpleTimer {
46 public:
49 #ifdef FARSA_WIN
50  QueryPerformanceFrequency( &frequency );
51  QueryPerformanceCounter( &baseCount );
52 #else
53  struct timeval tv;
54  gettimeofday( &tv, NULL );
55  lastTime = tv.tv_sec*1000000 + tv.tv_usec;
56 #endif
57  };
59  int tac() {
60 #ifdef FARSA_WIN
61  unsigned ticks;
62  QueryPerformanceCounter( &count );
63  count.QuadPart -= baseCount.QuadPart;
64  ticks = unsigned( count.QuadPart * LONGLONG (1000000) / frequency.QuadPart );
65  return ticks;
66 #else
67  struct timeval tv;
68  gettimeofday( &tv, NULL );
69  return (tv.tv_sec*1000000 + tv.tv_usec) - lastTime;
70 #endif
71  };
73  int tic() {
74 #ifdef FARSA_WIN
75  unsigned ticks;
76  QueryPerformanceCounter( &count );
77  count.QuadPart -= baseCount.QuadPart;
78  ticks = unsigned( count.QuadPart * LONGLONG (1000000) / frequency.QuadPart );
79  baseCount = count;
80  return ticks;
81 #else
82  struct timeval tv;
83  gettimeofday( &tv, NULL );
84  int ret = (tv.tv_sec*1000000 + tv.tv_usec) - lastTime;
85  lastTime = (tv.tv_sec*1000000 + tv.tv_usec);
86  return ret;
87 #endif
88  };
89 private:
90 #ifdef FARSA_WIN
91  LARGE_INTEGER count;
92  LARGE_INTEGER frequency;
93  LARGE_INTEGER baseCount;
94 #else
95  long int lastTime;
96 #endif
97 };
98 
99 } // end namespace farsa
100 
101 #endif