aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/include/Poco/Stopwatch.h
blob: 6bc27fc92be74a2f4859917acf5e066fdac0a074 (plain) (blame)
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
//
// Stopwatch.h
//
// Library: Foundation
// Package: DateTime
// Module:  Stopwatch
//
// Definition of the Stopwatch class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Foundation_Stopwatch_INCLUDED
#define Foundation_Stopwatch_INCLUDED


#include "Poco/Foundation.h"
#include "Poco/Clock.h"


namespace Poco {


class Foundation_API Stopwatch
	/// A simple facility to measure time intervals
	/// with microsecond resolution.
{
public:
	Stopwatch();
	~Stopwatch();

	void start();
		/// Starts (or restarts) the stopwatch.
		
	void stop();
		/// Stops or pauses the stopwatch.
	
	void reset();
		/// Resets the stopwatch.
		
	void restart();
		/// Resets and starts the stopwatch.
		
	Clock::ClockDiff elapsed() const;
		/// Returns the elapsed time in microseconds
		/// since the stopwatch started.
		
	int elapsedSeconds() const;
		/// Returns the number of seconds elapsed
		/// since the stopwatch started.

	static Clock::ClockVal resolution();
		/// Returns the resolution of the stopwatch.

private:
	Stopwatch(const Stopwatch&);
	Stopwatch& operator = (const Stopwatch&);

	Clock            _start;
	Clock::ClockDiff _elapsed;
	bool             _running;
};


//
// inlines
//
inline void Stopwatch::start()
{
	if (!_running)
	{
		_start.update();
		_running = true;
	}
}


inline void Stopwatch::stop()
{
	if (_running)
	{
		Clock current;
		_elapsed += current - _start;
		_running = false;
	}
}


inline int Stopwatch::elapsedSeconds() const
{
	return int(elapsed()/resolution());
}


inline Clock::ClockVal Stopwatch::resolution()
{
	return Clock::resolution();
}


} // namespace Poco


#endif // Foundation_Stopwatch_INCLUDED