-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.cc
65 lines (61 loc) · 1.39 KB
/
Time.cc
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
#include "Time.h"
#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#else
#ifdef __linux
#include <time.h>
#endif
#include <sys/time.h>
#endif
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
static bool initialized = false;
#ifdef _WIN32
static LARGE_INTEGER frequency;
static LARGE_INTEGER start_time;
#else
static struct timeval start_time;
#endif
void
Time::initialize()
{
initialized=true;
#ifdef _WIN32
if ( !QueryPerformanceFrequency( &frequency ) ) {
cerr << "QueryPerformanceFrequency failed" << endl;
exit( 1 );
}
if ( !QueryPerformanceCounter( &start_time ) ) {
cerr << "QueryPerformanceCounter failed" << endl;
exit( 1 );
}
#else
if(gettimeofday(&start_time, 0) != 0) {
cerr << "gettimeofday failed: " << strerror(errno) << endl;
exit( 1 );
}
#endif
}
double Time::currentSeconds()
{
if(!initialized)
initialize();
#ifdef _WIN32
LARGE_INTEGER now_time;
if ( !QueryPerformanceCounter( &now_time ) ) {
cerr << "QueryPerformanceCounter failed" << endl;
exit( 1 );
}
return static_cast< double >(now_time.QuadPart - start_time.QuadPart) / frequency.QuadPart;
#else
struct timeval now_time;
if(gettimeofday(&now_time, 0) != 0) {
cerr << "gettimeofday failed: " << strerror(errno) << endl;
exit( 1 );
}
return (now_time.tv_sec-start_time.tv_sec)+(now_time.tv_usec-start_time.tv_usec)*1.e-6;
#endif
}