-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimer.h
131 lines (114 loc) · 3.32 KB
/
timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Written by Richard David Cook
at Lawrence Livermore National Laboratory
Contact: [email protected]
See license.txt for information about usage.
Spoiler alert: it's GNU opensource.
*/
#ifndef TIMER_H
#define TIMER_H
/*!
VisIt has its quirks
*/
#ifdef RC_CPP_VISIT_BUILD
#include <visit-config.h>
// this does not work on chaos machines outside of VisIt, maybe it's a VisIt thing?
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#else
// if not in VisIt, then just include sys/time.h
#include <sys/time.h>
#endif
#include <stdio.h>
#include <ctime>
#ifdef WIN32
#include <sys/timeb.h>
#include <Winsock2.h>
#endif
#include <iostream>
#include <iomanip>
#include "stringutil.h"
#define INVALID_TIME_STRING "INVALID_TIME_STRING"
using namespace std;
string GetStandardTimeStringFromString(string s);
bool GetTimeFromString(string s, struct tm &tms);
int Progress(class timer &iTimer, double iNum, double iMax,
double &oPercent, double iPercentDelta,
double &oTime, double iTimeDelta, std::string iMsg);
// ========================================================================
// Get current date/time, format "%Y-%m-%d.%X" means YYYY-MM-DD.HH:mm:ss
static const inline std::string timestamp(const char *format=NULL) {
string fmtstring;
if (!format || string(format)=="") {
fmtstring = "%Y-%m-%d.%X";
} else {
fmtstring = format;
}
time_t now = time(0);
struct tm tstruct;
char buf[1000];
tstruct = *localtime(&now);
// Visit http://www.cplusplus.com/reference/clibrary/ctime/strftime/
// for more information about date/time format
strftime(buf, sizeof(buf), fmtstring.c_str(), &tstruct);
return buf;
}
// ========================================================================
/* thanks to
Kenneth Wilder
Department of Statistics
The University of Chicago
5734 South University Avenue
Chicago, IL 60637
Office: Eckhart 105
Phone: (773) 702-8325
Email: [email protected]
http://oldmill.uchicago.edu/~wilder/
*/
class timer
{
friend std::ostream& operator<<(std::ostream& os, timer& t);
private:
bool running;
double start_clock;
double acc_time;
double time_per_tick;
bool mUseWallTime;
public:
static std::string GetExactSecondsString(void) {
//return QTime::currentTime().toString("ssss.zzz").toStdString();
std::string s = doubleToString(GetExactSeconds(), 3);
int first = s.size() -8;
if (first<0) first = 0;
return s.substr(first, 8);
}
#ifndef WIN32
#ifndef RC_CPP_VISIT_BUILD
static
#endif
double GetExactSeconds(void) {
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + (double)t.tv_usec/1000000.0;
}
#else
static double GetExactSeconds(void) {
struct _timeb t;
_ftime(&t);
return t.time + (double)t.millitm/1000.0;
}
#endif
// 'running' is initially false. A timer needs to be explicitly started
// using 'start' or 'restart'
timer() : running(false), start_clock(0), acc_time(0), mUseWallTime(true) {
time_per_tick = 1.0/(double)CLOCKS_PER_SEC;
}
void useWallTime(bool use=true) {mUseWallTime=use;}
double elapsed_time(void);
double total_time(void);
void start(const char* msg = 0);
void restart(const char* msg = 0);
void stop(const char* msg = 0);
void check(const char* msg = 0);
}; // class timer
#endif // TIMER_H