FORM  4.3
mytime.cc
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 
5 // A timing routine for debugging. Only on Unix (where sys/time.h is available).
6 #ifdef UNIX
7 
8 #include <sys/time.h>
9 #include <cstdlib>
10 #include <cstdio>
11 #include <string>
12 
13 #ifndef timersub
14 /* timersub is not in POSIX, but presents on most BSD derivatives.
15  This implementation is borrowed from glibc. (TU 23 Oct 2011) */
16 #define timersub(a, b, result) \
17  do { \
18  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
19  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
20  if ((result)->tv_usec < 0) { \
21  --(result)->tv_sec; \
22  (result)->tv_usec += 1000000; \
23  } \
24  } while (0)
25 #endif
26 
27 bool starttime_set = false;
28 timeval starttime;
29 
30 double thetime () {
31  if (!starttime_set) {
32  gettimeofday(&starttime,NULL);
33  starttime_set=true;
34  }
35 
36  timeval now,diff;
37  gettimeofday(&now,NULL);
38  timersub(&now,&starttime,&diff);
39  return diff.tv_sec+diff.tv_usec/1000000.0;
40 }
41 
42 std::string thetime_str() {
43  char res[10];
44  snprintf (res,10,"%.4lf", thetime());
45  return res;
46 }
47 
48 #endif // UNIX