accelerInt  v0.1
timer.h
Go to the documentation of this file.
1 
20 #ifndef TIMER_H
21 #define TIMER_H
22 
23 #include <stdlib.h>
24 
25 #ifdef WIN32
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #else
29  #ifndef __USE_BSD
30  #define __USE_BSD
31  #endif
32 #include <time.h>
33 #include <sys/time.h>
34 #endif
35 
36 #ifdef WIN32
37 double PCFreq = 0.0;
38 __int64 timerStart = 0;
39 #else
40 struct timeval timerStart;
41 #endif
42 
43 void StartTimer()
44 {
45 #ifdef WIN32
46  LARGE_INTEGER li;
47  if(!QueryPerformanceFrequency(&li))
48  printf("QueryPerformanceFrequency failed!\n");
49 
50  PCFreq = (double)li.QuadPart/1000.0;
51 
52  QueryPerformanceCounter(&li);
53  timerStart = li.QuadPart;
54 #else
55  gettimeofday(&timerStart, NULL);
56 #endif
57 }
58 
59 // time elapsed in ms
60 double GetTimer()
61 {
62 #ifdef WIN32
63  LARGE_INTEGER li;
64  QueryPerformanceCounter(&li);
65  return (double)(li.QuadPart-timerStart)/PCFreq;
66 #else
67  struct timeval timerStop, timerElapsed;
68  gettimeofday(&timerStop, NULL);
69  timersub(&timerStop, &timerStart, &timerElapsed);
70  return timerElapsed.tv_sec*1000.0+timerElapsed.tv_usec/1000.0;
71 #endif
72 }
73 
74 #endif // TIMER_H
void StartTimer()
Definition: timer.h:43
double GetTimer()
Definition: timer.h:60
struct timeval timerStart
Definition: timer.h:40