WSL/SLF GitLab Repository

Timer.cc 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***********************************************************************************/
/*                   Copyright GridGroup, EIA-FR 2010                              */
/*  Copyright 2010 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
/***********************************************************************************/
/* This file is part of MeteoIO.
    MeteoIO is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MeteoIO is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with MeteoIO.  If not, see <http://www.gnu.org/licenses/>.
*/
//AUTHORS: Tuan Anh Nguyen (original implementation in popc)
//         Mathias Bavay (port and rewrite for Alpine3D, then MeteoIO)

#include <stdio.h>
23
#ifdef WIN32
24
25
26
27
28
	#include <windows.h>
	#undef max
	#undef min
#else
	#include <sys/time.h>
29
	#include <sys/resource.h>
30
31
32
#endif

#include "Timer.h"
33
34
35
36

namespace mio {

/**
37
* @brief Default constructor.
38
39
* Initialize internal variables. It does NOT start timing.
*/
40
41
Timer::Timer()
     : start_point(0.), elapsed(0.), isRunning(false) {}
42
43

/**
44
* @brief Start the timer.
45
46
47
*/
void Timer::start() {
	if (!isRunning) {
48
49
		isRunning = true;
		start_point = getCurrentTime();
50
51
52
53
	}
}

/**
54
55
56
57
* @brief Reset and start the timer.
*/
void Timer::restart() {
	reset();
58
	isRunning = true;
59
60
61
62
}

/**
* @brief Stop the timer.
63
* It can be restarted afterward, adding time to what was already timed.
64
65
66
*/
void Timer::stop() {
	if (isRunning) {
67
		elapsed += static_cast<double>( getCurrentTime()-start_point );
68
		isRunning = false;
69
70
71
72
73
74
75
	}
}

/**
* @brief Reset the timer to zero.
*/
void Timer::reset() {
76
	elapsed = 0.;
77
	start_point = getCurrentTime();
78
79
80
}

/**
81
* @brief Get total elapsed time.
82
* It returns the sum of all the elapsed time between all the start/stop sessions since
83
* the timer was created or the last call to reset. Time is in seconds with microsecond resolution.
84
*/
85
double Timer::getElapsed() const {
86
	if (isRunning) {
87
		return elapsed + static_cast<double>(getCurrentTime()-start_point);
88
89
90
91
	}
	return elapsed;
}

92
#ifdef WIN32
93
long double Timer::getCurrentTime() const {
94
95
96
97
98
99
100
101
102
103
	SYSTEMTIME systemTime;
	GetSystemTime( &systemTime );

	FILETIME fileTime;
	SystemTimeToFileTime( &systemTime, &fileTime );

	ULARGE_INTEGER uli;
	uli.LowPart = fileTime.dwLowDateTime;
	uli.HighPart = fileTime.dwHighDateTime;

104
	const ULONGLONG units_convert = 10000*1000ULL; //it gives the time since 1 January 1601 (UTC) in units of 100ns
105
	const ULONGLONG offset_to_epoch = 11644473600ULL; //offset in seconds to Unix epoch, 134774 days * 24*3600
106
	return static_cast<long double>(uli.QuadPart - offset_to_epoch*units_convert) / units_convert;
107
108
}
#else
109
long double Timer::getCurrentTime() const {
110
111
	timeval tp;
	gettimeofday(&tp,NULL);
112
	const long double t = static_cast<long double>(tp.tv_sec) + static_cast<long double>(tp.tv_usec)*1.0e-6;
113
114
	return t;
}
115
#endif
116
117
118

} //namespace