added string2hex prog (usefull for wep key)
[my-code/beginners.git] / time-example.c
1 /*
2  * example of gettimeofday usage
3  *
4  */
5
6 #include <stdio.h>
7 #include <sys/time.h>
8 #include <time.h> // only needed for time command (random seed)
9 #include <stdlib.h>
10
11 int main(int argc,char **argv) {
12
13   struct timeval foo1;
14   struct timeval foo2;
15   int sec,usec;
16   int i;
17
18   srand(time(NULL)); // random seed
19
20   printf("time 1:\n");
21   gettimeofday(&foo1,NULL);
22   printf("seconds: %d \t usec: %d\n",(int)foo1.tv_sec,(int)foo1.tv_usec);
23
24   while(i<rand()) i++;
25
26   gettimeofday(&foo2,NULL);
27   printf("time 2:\n");
28   printf("seconds: %d \t usec: %d\n\n",(int)foo2.tv_sec,(int)foo2.tv_usec);
29
30   sec=foo2.tv_sec-foo1.tv_sec;
31   sec=(foo2.tv_usec<foo1.tv_usec)?sec-1:sec;
32   usec=(foo2.tv_usec<foo1.tv_usec)?1000000-foo1.tv_usec+foo2.tv_usec
33                                   :foo2.tv_usec-foo1.tv_usec;
34   printf("delta t = %d sec %d usec\n",sec,usec);
35
36   return 1;
37 }