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  #include <unistd.h>
30 #endif
31 
32 namespace farsa {
33 
39 inline void FARSA_UTIL_TEMPLATE msleep(unsigned int msec)
40 {
41 #ifdef FARSA_WIN
42  Sleep(msec);
43 #else
44  usleep(msec * 1000);
45 #endif
46 }
47 
60 class FARSA_UTIL_TEMPLATE SimpleTimer {
61 public:
64 #ifdef FARSA_WIN
65  QueryPerformanceFrequency( &frequency );
66  QueryPerformanceCounter( &baseCount );
67 #else
68  struct timeval tv;
69  gettimeofday( &tv, NULL );
70  lastTime = tv.tv_sec*1000000 + tv.tv_usec;
71 #endif
72  };
74  int tac() {
75 #ifdef FARSA_WIN
76  unsigned ticks;
77  QueryPerformanceCounter( &count );
78  count.QuadPart -= baseCount.QuadPart;
79  ticks = unsigned( count.QuadPart * LONGLONG (1000000) / frequency.QuadPart );
80  return ticks;
81 #else
82  struct timeval tv;
83  gettimeofday( &tv, NULL );
84  return (tv.tv_sec*1000000 + tv.tv_usec) - lastTime;
85 #endif
86  };
88  int tic() {
89 #ifdef FARSA_WIN
90  unsigned ticks;
91  QueryPerformanceCounter( &count );
92  count.QuadPart -= baseCount.QuadPart;
93  ticks = unsigned( count.QuadPart * LONGLONG (1000000) / frequency.QuadPart );
94  baseCount = count;
95  return ticks;
96 #else
97  struct timeval tv;
98  gettimeofday( &tv, NULL );
99  int ret = (tv.tv_sec*1000000 + tv.tv_usec) - lastTime;
100  lastTime = (tv.tv_sec*1000000 + tv.tv_usec);
101  return ret;
102 #endif
103  };
104 private:
105 #ifdef FARSA_WIN
106  LARGE_INTEGER count;
107  LARGE_INTEGER frequency;
108  LARGE_INTEGER baseCount;
109 #else
110  long int lastTime;
111 #endif
112 };
113 
114 } // end namespace farsa
115 
116 #endif
This file contains the common type defitions used on the whole framework.
void FARSA_UTIL_TEMPLATE msleep(unsigned int msec)
A simple function to sleep for the given amount of milliseconds.
Definition: simpletimer.h:39
A macro to deprecate functions.
SimpleTimer object.
Definition: simpletimer.h:60
int tac()
return microsecond elapsed from last tic() call
Definition: simpletimer.h:74
SimpleTimer()
Construct the timer.
Definition: simpletimer.h:63
int tic()
return microsecond elapsed from last tic() call
Definition: simpletimer.h:88