Add a stopwatch utility class with lap times.

Allows to meassure the duration of something that started at a
point of time, with some small convenience methods.
This commit is contained in:
Klaas Freitag 2014-03-26 18:00:02 +01:00
parent 505429b582
commit 7b84defd56
2 changed files with 72 additions and 0 deletions

View file

@ -490,4 +490,58 @@ bool Utility::isLinux()
#endif
}
# define STOPWATCH_END_TAG QLatin1String("_STOPWATCH_END")
void Utility::StopWatch::start()
{
_startTime = QDateTime::currentDateTime();
_timer.start();
}
void Utility::StopWatch::stop()
{
addLapTime(QLatin1String(STOPWATCH_END_TAG));
_timer.invalidate();
}
quint64 Utility::StopWatch::addLapTime( const QString& lapName )
{
if( !_timer.isValid() ) {
start();
}
quint64 re = _timer.elapsed();
QPair<QString, quint64> p(lapName, re);
_lapTimes.append(p);
return re;
}
QDateTime Utility::StopWatch::startTime()
{
return _startTime;
}
QDateTime Utility::StopWatch::timeOfLap( const QString& lapName )
{
quint64 t = durationOfLap(lapName);
if( t ) {
QDateTime re(_startTime);
return re.addMSecs(t);
}
return QDateTime();
}
quint64 Utility::StopWatch::durationOfLap( const QString& lapName )
{
QPair<QString, quint64> lapPair;
foreach( lapPair, _lapTimes ) {
if( lapPair.first == lapName ) {
return lapPair.second;
}
}
return 0;
}
} // namespace Mirall

View file

@ -18,6 +18,8 @@
#include <QString>
#include <QByteArray>
#include <QDateTime>
#include <QPair>
#include <QElapsedTimer>
class QWidget;
@ -65,6 +67,22 @@ namespace Utility
bool isMac();
bool isUnix();
bool isLinux(); // use with care
class StopWatch {
private:
QList<QPair<QString, quint64> > _lapTimes;
QDateTime _startTime;
QElapsedTimer _timer;
public:
void start();
void stop();
quint64 addLapTime( const QString& lapName );
// out helpers, return the masured times.
QDateTime startTime();
QDateTime timeOfLap( const QString& lapName );
quint64 durationOfLap( const QString& lapName );
};
}
}