1
|
1 // Precise timer routines for LINUX (C) LGB & A'rpi/ASTRAL
|
|
2
|
|
3 #include <unistd.h>
|
|
4 #include <sys/time.h>
|
|
5
|
99
|
6 // Returns current time in microseconds
|
1
|
7 unsigned int GetTimer(){
|
|
8 struct timeval tv;
|
|
9 struct timezone tz;
|
|
10 // float s;
|
|
11 gettimeofday(&tv,&tz);
|
|
12 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
|
|
13 return (tv.tv_sec*1000000+tv.tv_usec);
|
|
14 }
|
|
15
|
|
16 static unsigned int RelativeTime=0;
|
|
17
|
|
18 // Returns time spent between now and last call in seconds
|
|
19 float GetRelativeTime(){
|
|
20 unsigned int t,r;
|
|
21 t=GetTimer();
|
|
22 // t*=16;printf("time=%ud\n",t);
|
|
23 r=t-RelativeTime;
|
|
24 RelativeTime=t;
|
|
25 return (float)r * 0.000001F;
|
|
26 }
|
|
27
|
|
28 // Initialize timer, must be called at least once at start
|
|
29 void InitTimer(){
|
|
30 GetRelativeTime();
|
|
31 }
|
|
32
|
|
33
|
|
34 #if 0
|
|
35 void main(){
|
|
36 float t=0;
|
|
37 InitTimer();
|
|
38 while(1){ t+=GetRelativeTime();printf("time= %10.6f\r",t);fflush(stdout); }
|
|
39 }
|
|
40 #endif
|
|
41
|