6957bc9cfe2dc760937fbc70d6749f7d97960e84
[yaz-moved-to-github.git] / src / timing.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: timing.c,v 1.1 2007-01-03 13:46:18 adam Exp $
6  */
7
8 /**
9  * \file timing.c
10  * \brief Timing Utilities
11  */
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <stdlib.h>
18
19 #if HAVE_SYS_TIMES_H
20 #include <sys/times.h>
21 #endif
22 #if HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25 #include <time.h>
26
27 #include <yaz/xmalloc.h>
28 #include <yaz/timing.h>
29
30 struct yaz_timing {
31 #if HAVE_SYS_TIMES_H
32     struct tms tms1, tms2;
33 #endif
34 #if HAVE_SYS_TIME_H
35     struct timeval start_time, end_time;
36 #endif
37     double real_sec, user_sec, sys_sec;
38 };
39
40 yaz_timing_t yaz_timing_create(void)
41 {
42     yaz_timing_t t = xmalloc(sizeof(*t));
43     yaz_timing_start(t);
44     return t;
45 }
46
47 void yaz_timing_start(yaz_timing_t t)
48 {
49 #if HAVE_SYS_TIMES_H
50     times(&t->tms1);
51 #endif
52 #if HAVE_SYS_TIME_H
53     gettimeofday(&t->start_time, 0);
54 #endif
55     t->real_sec = -1.0;
56     t->user_sec = -1.0;
57     t->sys_sec = -1.0;
58 }
59
60 void yaz_timing_stop(yaz_timing_t t)
61 {
62     t->real_sec = 0.0;
63     t->user_sec = 0.0;
64     t->sys_sec = 0.0;
65 #if HAVE_SYS_TIMES_H
66     times(&t->tms2);
67     
68     t->user_sec = (double) (t->tms2.tms_utime - t->tms1.tms_utime)/100;
69     t->sys_sec = (double) (t->tms2.tms_stime - t->tms1.tms_stime)/100;
70 #endif
71 #if HAVE_SYS_TIME_H
72     gettimeofday(&t->end_time, 0);
73     t->real_sec = ((t->end_time.tv_sec - t->start_time.tv_sec) * 1000000.0 +
74                    t->end_time.tv_usec - t->start_time.tv_usec) / 1000000;
75     
76 #endif
77 }
78
79 double yaz_timing_get_real(yaz_timing_t t)
80 {
81     return t->real_sec;
82 }
83
84 double yaz_timing_get_user(yaz_timing_t t)
85 {
86     return t->user_sec;
87 }
88
89 double yaz_timing_get_sys(yaz_timing_t t)
90 {
91     return t->sys_sec;
92 }
93
94 void yaz_timing_destroy(yaz_timing_t *tp)
95 {
96     if (*tp)
97     {
98         xfree(*tp);
99         *tp = 0;
100     }
101 }
102
103 /*
104  * Local variables:
105  * c-basic-offset: 4
106  * indent-tabs-mode: nil
107  * End:
108  * vim: shiftwidth=4 tabstop=8 expandtab
109  */
110