]> mj.ucw.cz Git - libucw.git/blob - lib/timer.c
Yet another bug in the pattern matcher fixed.
[libucw.git] / lib / timer.c
1 /*
2  *      Sherlock Library -- Execution Timing
3  *
4  *      (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/time.h>
10
11 #include "lib.h"
12
13 static struct timeval last_tv;
14
15 uns
16 get_timer(void)
17 {
18   struct timeval tv;
19   uns diff;
20
21   gettimeofday(&tv, NULL);
22   if (tv.tv_sec < last_tv.tv_sec
23       || tv.tv_sec == last_tv.tv_sec && tv.tv_usec < last_tv.tv_usec)
24     diff = 0;
25   else
26     {
27       if (tv.tv_sec == last_tv.tv_sec)
28         diff = (tv.tv_usec - last_tv.tv_usec + 500) / 1000;
29       else
30         {
31           diff = 1000 * (tv.tv_sec - last_tv.tv_sec - 1);
32           diff += (1000500 - last_tv.tv_usec + tv.tv_usec) / 1000;
33         }
34     }
35   last_tv = tv;
36   return diff;
37 }
38
39 void
40 init_timer(void)
41 {
42   gettimeofday(&last_tv, NULL);
43 }