2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
// Stopwatch.h
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: DateTime
|
|
|
|
// Module: Stopwatch
|
|
|
|
//
|
|
|
|
// Definition of the Stopwatch class.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_Stopwatch_INCLUDED
|
|
|
|
#define Foundation_Stopwatch_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
2013-12-12 18:33:29 +01:00
|
|
|
#include "Poco/Clock.h"
|
2012-04-29 20:52:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2013-12-12 18:33:29 +01:00
|
|
|
Clock::ClockDiff elapsed() const;
|
2012-04-29 20:52:25 +02:00
|
|
|
/// Returns the elapsed time in microseconds
|
|
|
|
/// since the stopwatch started.
|
|
|
|
|
|
|
|
int elapsedSeconds() const;
|
|
|
|
/// Returns the number of seconds elapsed
|
|
|
|
/// since the stopwatch started.
|
|
|
|
|
2013-12-12 18:33:29 +01:00
|
|
|
static Clock::ClockVal resolution();
|
2012-04-29 20:52:25 +02:00
|
|
|
/// Returns the resolution of the stopwatch.
|
|
|
|
|
|
|
|
private:
|
|
|
|
Stopwatch(const Stopwatch&);
|
|
|
|
Stopwatch& operator = (const Stopwatch&);
|
|
|
|
|
2013-12-12 18:33:29 +01:00
|
|
|
Clock _start;
|
|
|
|
Clock::ClockDiff _elapsed;
|
|
|
|
bool _running;
|
2012-04-29 20:52:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
|
|
|
inline void Stopwatch::start()
|
|
|
|
{
|
|
|
|
if (!_running)
|
|
|
|
{
|
|
|
|
_start.update();
|
|
|
|
_running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Stopwatch::stop()
|
|
|
|
{
|
|
|
|
if (_running)
|
|
|
|
{
|
2013-12-12 18:33:29 +01:00
|
|
|
Clock current;
|
2012-04-29 20:52:25 +02:00
|
|
|
_elapsed += current - _start;
|
|
|
|
_running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int Stopwatch::elapsedSeconds() const
|
|
|
|
{
|
|
|
|
return int(elapsed()/resolution());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-12 18:33:29 +01:00
|
|
|
inline Clock::ClockVal Stopwatch::resolution()
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
2013-12-12 18:33:29 +01:00
|
|
|
return Clock::resolution();
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_Stopwatch_INCLUDED
|