GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-7413740dd8
rtimer.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * MODULE: iostream
4  *
5 
6  * COPYRIGHT (C) 2007 Laura Toma
7  *
8  *
9 
10  * Iostream is a library that implements streams, external memory
11  * sorting on streams, and an external memory priority queue on
12  * streams. These are the fundamental components used in external
13  * memory algorithms.
14 
15  * Credits: The library was developed by Laura Toma. The kernel of
16  * class STREAM is based on the similar class existent in the GPL TPIE
17  * project developed at Duke University. The sorting and priority
18  * queue have been developed by Laura Toma based on communications
19  * with Rajiv Wickremesinghe. The library was developed as part of
20  * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
21  * Rajiv Wickremesinghe as part of the Terracost project.
22 
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29 
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * General Public License for more details. *
34  * **************************************************************************/
35 
36 #ifndef RTIMER_H
37 #define RTIMER_H
38 
39 #ifdef __MINGW32__
40 
41 #include <time.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <strings.h>
45 
46 typedef struct {
47  time_t tv1, tv2;
48 } Rtimer;
49 
50 #define rt_start(rt) \
51  if ((time(&(rt.tv1)) == ((time_t)-1))) { \
52  perror("time"); \
53  exit(1); \
54  }
55 
56 /* doesn't really stop, just updates endtimes */
57 #define rt_stop(rt) \
58  if ((time(&(rt.tv2)) == ((time_t)-1))) { \
59  perror("time"); \
60  exit(1); \
61  }
62 
63 #define rt_u_useconds(rt) rt_w_useconds(rt)
64 
65 #define rt_s_useconds(rt) rt_w_useconds(rt)
66 
67 #define rt_w_useconds(rt) (1.0e6 * (rt.tv2 - rt.tv1))
68 
69 #else /* __MINGW32__ */
70 
71 #include <sys/time.h>
72 #include <sys/resource.h>
73 #include <stdio.h>
74 #include <string.h>
75 #include <strings.h>
76 
77 typedef struct {
78  struct rusage rut1, rut2;
79  struct timeval tv1, tv2;
80 } Rtimer;
81 
82 #define rt_start(rt) \
83  if ((getrusage(RUSAGE_SELF, &rt.rut1) < 0) || \
84  (gettimeofday(&(rt.tv1), NULL) < 0)) { \
85  perror("rusage/gettimeofday"); \
86  exit(1); \
87  }
88 
89 /* doesn't really stop, just updates endtimes */
90 #define rt_stop(rt) \
91  if ((getrusage(RUSAGE_SELF, &rt.rut2) < 0) || \
92  (gettimeofday(&(rt.tv2), NULL) < 0)) { \
93  perror("rusage/gettimeofday"); \
94  exit(1); \
95  }
96 
97 #define rt_u_useconds(rt) \
98  (((double)rt.rut2.ru_utime.tv_usec + \
99  (double)rt.rut2.ru_utime.tv_sec * 1000000) - \
100  ((double)rt.rut1.ru_utime.tv_usec + \
101  (double)rt.rut1.ru_utime.tv_sec * 1000000))
102 
103 #define rt_s_useconds(rt) \
104  (((double)rt.rut2.ru_stime.tv_usec + \
105  (double)rt.rut2.ru_stime.tv_sec * 1000000) - \
106  ((double)rt.rut1.ru_stime.tv_usec + \
107  (double)rt.rut1.ru_stime.tv_sec * 1000000))
108 
109 #define rt_w_useconds(rt) \
110  (((double)rt.tv2.tv_usec + (double)rt.tv2.tv_sec * 1000000) - \
111  ((double)rt.tv1.tv_usec + (double)rt.tv1.tv_sec * 1000000))
112 
113 #endif /* __MINGW32__ */
114 
115 /* not required to be called, but makes values print as 0.
116  obviously a hack */
117 #define rt_zero(rt) bzero(&(rt), sizeof(Rtimer));
118 
119 #define rt_seconds(rt) (rt_w_useconds(rt) / 1000000)
120 
121 #define rt_sprint(buf, rt) rt_sprint_safe(buf, rt)
122 
123 char *rt_sprint_safe(char *buf, Rtimer rt);
124 
125 #endif /* RTIMER_H */
char * rt_sprint_safe(char *buf, Rtimer rt)
Definition: rtimer.cpp:45
Definition: rtimer.h:77