WSL/SLF GitLab Repository

Timer.h 2.33 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
23
24
25
26
27
28
29
30
31
32
33
/***********************************************************************************/
/*                   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)

#ifndef __TIMER_H__
#define __TIMER_H__

#include <meteoio/IOExceptions.h>

#include <string>
#include <sstream>
#include <utility>
#include <iomanip>
#include <iostream>
#include <ctime>

34
35
36
37
#ifndef WIN32
	#include <sys/resource.h>
#endif

38
39
40
41
namespace mio {

/**
 * @class Timer
42
43
44
45
 * @brief Time code execution with at least 1 us resolution.
 * The time resolution can be stored up to .1 ns resolution, but is measured to the following accuracy:
 *    - 1 us on Posix systems (Linux, osX, BSD);
 *    - 1 ns on Windows.
46
47
48
49
50
 */
class Timer {
public:
	Timer();
	void start();
51
	void restart();
52
53
	void stop();
	void reset();
54
	double getElapsed() const;
55

56
protected:
57
	long double getCurrentTime() const;
58

59
	long double start_point;
60
61
62
63
	double elapsed;
	bool isRunning;
};

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef WIN32
/**
 * @class UsageTimer
 * @author Thomas Egger
 */
class UsageTimer {
 public:
	UsageTimer();
	void start();
	void restart();
	void stop();
	void reset();

	double getElapsed();
	double getElapsedUserTime();
	double getElapsedSystemTime();
80

81
82
83
84
85
86
87
 protected:
	void getElapsedTimes();

	static const int who;

	struct rusage start_usage, current_usage;
	double user_time, sys_time, elapsed;
88
	bool is_running;
89
90
91
};
#endif

92
93
} //end namespace mio
#endif