win32 get real implemented (timings that is).
[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.3 2007-01-05 12:40:05 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 #ifdef WIN32
18 #include <windows.h>
19 #endif
20 #include <stdlib.h>
21
22 #if HAVE_SYS_TIMES_H
23 #include <sys/times.h>
24 #endif
25 #if HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #include <time.h>
29
30 #include <yaz/xmalloc.h>
31 #include <yaz/timing.h>
32
33 struct yaz_timing {
34 #if HAVE_SYS_TIMES_H
35     struct tms tms1, tms2;
36 #endif
37 #if HAVE_SYS_TIME_H
38     struct timeval start_time, end_time;
39 #endif
40 #ifdef WIN32
41     ULONGLONG start_time, end_time;
42 #endif
43     double real_sec, user_sec, sys_sec;
44 };
45
46 yaz_timing_t yaz_timing_create(void)
47 {
48     yaz_timing_t t = xmalloc(sizeof(*t));
49     yaz_timing_start(t);
50     return t;
51 }
52
53 #ifdef WIN32
54 static void get_date_as_largeinteger(ULONGLONG *lp)
55 {
56     FILETIME f;
57     ULARGE_INTEGER li;
58     GetSystemTimeAsFileTime(&f);
59     li.LowPart = f.dwLowDateTime;
60     li.HighPart = f.dwHighDateTime;
61
62     *lp = li.QuadPart;
63 }
64 #endif
65
66 void yaz_timing_start(yaz_timing_t t)
67 {
68 #if HAVE_SYS_TIMES_H
69     times(&t->tms1);
70     t->user_sec = 0.0;
71     t->sys_sec = 0.0;
72 #else
73     t->user_sec = -1.0;
74     t->sys_sec = -1.0;
75 #endif
76     t->real_sec = -1.0;
77 #if HAVE_SYS_TIME_H
78     gettimeofday(&t->start_time, 0);
79     t->real_sec = 0.0;
80 #endif
81 #ifdef WIN32
82     get_date_as_largeinteger(&t->start_time);
83     t->real_sec = 0.0;
84 #endif
85 }
86
87 void yaz_timing_stop(yaz_timing_t t)
88 {
89 #if HAVE_SYS_TIMES_H
90     times(&t->tms2);
91     
92     t->user_sec = (double) (t->tms2.tms_utime - t->tms1.tms_utime)/100;
93     t->sys_sec = (double) (t->tms2.tms_stime - t->tms1.tms_stime)/100;
94 #endif
95 #if HAVE_SYS_TIME_H
96     gettimeofday(&t->end_time, 0);
97     t->real_sec = ((t->end_time.tv_sec - t->start_time.tv_sec) * 1000000.0 +
98                    t->end_time.tv_usec - t->start_time.tv_usec) / 1000000;
99     
100 #endif
101 #ifdef WIN32
102     get_date_as_largeinteger(&t->end_time);
103     t->real_sec = (double) (t->end_time - t->start_time) / 10000000.0;
104 #endif
105 }
106
107 double yaz_timing_get_real(yaz_timing_t t)
108 {
109     return t->real_sec;
110 }
111
112 double yaz_timing_get_user(yaz_timing_t t)
113 {
114     return t->user_sec;
115 }
116
117 double yaz_timing_get_sys(yaz_timing_t t)
118 {
119     return t->sys_sec;
120 }
121
122 void yaz_timing_destroy(yaz_timing_t *tp)
123 {
124     if (*tp)
125     {
126         xfree(*tp);
127         *tp = 0;
128     }
129 }
130
131 /*
132  * Local variables:
133  * c-basic-offset: 4
134  * indent-tabs-mode: nil
135  * End:
136  * vim: shiftwidth=4 tabstop=8 expandtab
137  */
138