Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #pragma once
00017 #ifndef ZORBA_UTIL_TIME_H
00018 #define ZORBA_UTIL_TIME_H
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 namespace zorba
00051 {
00052
00053 namespace time
00054 {
00055
00056
00057
00058
00059
00060
00061
00062 #if (defined(ZORBA_HAVE_CLOCKGETTIME_FUNCTION) & defined(_POSIX_CPUTIME))
00063
00064 #include <time.h>
00065
00066 typedef struct timespec cputime;
00067
00068 inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
00069 {
00070 return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
00071 ((t1.tv_nsec - t0.tv_nsec) / 1000000.0);
00072 }
00073
00074 inline void get_current_cputime (cputime& t)
00075 {
00076 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
00077 }
00078
00079 #elif defined(ZORBA_HAVE_RUSAGE_FUNCTION)
00080
00081 #include <sys/time.h>
00082 #include <sys/resource.h>
00083
00084 typedef struct timeval cputime;
00085
00086 inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
00087 {
00088 return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
00089 ((t1.tv_usec - t0.tv_usec) / 1000.0);
00090 }
00091
00092 inline void get_current_cputime (cputime& t)
00093 {
00094 struct rusage ru;
00095 getrusage (RUSAGE_SELF, &ru);
00096 t = ru.ru_utime;
00097 }
00098
00099 #else
00100
00101 #include <time.h>
00102
00103 typedef clock_t cputime;
00104
00105 inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
00106 {
00107 return (double) (t1 - t0) / (CLOCKS_PER_SEC / 1000);
00108 }
00109
00110 inline void get_current_cputime (cputime& t)
00111 {
00112 t = clock ();
00113 }
00114
00115 #endif
00116
00117
00118
00119
00120
00121
00122
00123
00124 #if defined(ZORBA_HAVE_CLOCKGETTIME_FUNCTION)
00125
00126 #include <time.h>
00127
00128 typedef struct timespec walltime;
00129
00130 inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
00131 {
00132 return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
00133 ((t1.tv_nsec - t0.tv_nsec) / 1000000.0);
00134 }
00135
00136 inline void get_current_walltime (walltime& t)
00137 {
00138 #ifdef _POSIX_MONOTONIC_CLOCK
00139 clock_gettime(CLOCK_MONOTONIC, &t);
00140 #else
00141 clock_gettime(CLOCK_REALTIME, &t);
00142 #endif
00143 }
00144
00145 inline long get_walltime_in_millis(const walltime& t)
00146 {
00147 return t.tv_sec * 1000 + t.tv_nsec / 1000000;
00148 }
00149
00150 #elif defined(WIN32)
00151
00152
00153
00154
00155
00156
00157 #include <sys/timeb.h>
00158
00159 #ifdef WINCE
00160 typedef struct timeb walltime;
00161 #else
00162 typedef struct _timeb walltime;
00163 #endif
00164
00165 inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
00166 {
00167 return ((t1.time - t0.time) * 1000.0) + (t1.millitm - t0.millitm);
00168 }
00169
00170 inline void get_current_walltime (walltime& t)
00171 {
00172 #ifdef WINCE
00173 ftime(&t);
00174 #else
00175 _ftime_s(&t);
00176 #endif
00177 }
00178
00179 inline long get_walltime_in_millis(const walltime& t)
00180 {
00181 return t.time * 1000 + t.millitm;
00182 }
00183
00184 #else
00185
00186 #include <time.h>
00187 #include <sys/time.h>
00188
00189 typedef struct timeval walltime;
00190
00191 inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
00192 {
00193 return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
00194 ((t1.tv_usec - t0.tv_usec) / 1000.0);
00195 }
00196
00197 inline void get_current_walltime (walltime& t)
00198 {
00199 gettimeofday(&t, NULL);
00200 }
00201
00202 inline long get_walltime_in_millis(const walltime& t)
00203 {
00204 return t.tv_sec * 1000 + t.tv_usec / 1000;
00205 }
00206
00207 #endif
00208
00209 }
00210
00211 }
00212
00213 #endif
00214
00215
00216
00217
00218
00219
00220