summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBradley T. Hughes <bradley.hughes@nokia.com>2010-09-30 15:29:40 (GMT)
committerBradley T. Hughes <bradley.hughes@nokia.com>2010-12-20 14:23:14 (GMT)
commite679392978c21266ec0bf6f960ab8c5f0621e18b (patch)
tree292f2e214acb4cb9b8f3e89a102d65cb7d9260a9
parentdaba2c507ad42c66dafa6a29cffa94e9641e0c58 (diff)
downloadQt-e679392978c21266ec0bf6f960ab8c5f0621e18b.zip
Qt-e679392978c21266ec0bf6f960ab8c5f0621e18b.tar.gz
Qt-e679392978c21266ec0bf6f960ab8c5f0621e18b.tar.bz2
Add QElapsedTimer::nsecsElapsed() const
Allow sub-millisecond resolution on systems where it is possible. On UNIX, with monotonic clock support we get full nanosecond resolution, microsecond otherwise. On Windows we convert the performance counters to nanoseconds if availble, otherwise we only have millisecond resolution. On Mac, the mach time is converted to nanoseconds. On Symbian, we have microsecond resolution. Reviewed-by: joao
-rw-r--r--src/corelib/tools/qelapsedtimer.h1
-rw-r--r--src/corelib/tools/qelapsedtimer_generic.cpp16
-rw-r--r--src/corelib/tools/qelapsedtimer_mac.cpp6
-rw-r--r--src/corelib/tools/qelapsedtimer_symbian.cpp22
-rw-r--r--src/corelib/tools/qelapsedtimer_unix.cpp11
-rw-r--r--src/corelib/tools/qelapsedtimer_win.cpp20
-rw-r--r--tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp7
7 files changed, 64 insertions, 19 deletions
diff --git a/src/corelib/tools/qelapsedtimer.h b/src/corelib/tools/qelapsedtimer.h
index b996f6a..4118389 100644
--- a/src/corelib/tools/qelapsedtimer.h
+++ b/src/corelib/tools/qelapsedtimer.h
@@ -68,6 +68,7 @@ public:
void invalidate();
bool isValid() const;
+ qint64 nsecsElapsed() const;
qint64 elapsed() const;
bool hasExpired(qint64 timeout) const;
diff --git a/src/corelib/tools/qelapsedtimer_generic.cpp b/src/corelib/tools/qelapsedtimer_generic.cpp
index 85986e6..740f496 100644
--- a/src/corelib/tools/qelapsedtimer_generic.cpp
+++ b/src/corelib/tools/qelapsedtimer_generic.cpp
@@ -103,6 +103,22 @@ qint64 QElapsedTimer::restart()
return t1 - old;
}
+/*! \since 4.8
+
+ Returns the number of nanoseconds since this QElapsedTimer was last
+ started. Calling this function in a QElapsedTimer that was invalidated
+ will result in undefined results.
+
+ On platforms that do not provide nanosecond resolution, the value returned
+ will be the best estimate available.
+
+ \sa start(), restart(), hasExpired(), invalidate()
+*/
+qint64 QElapsedTimer::nsecsElapsed() const
+{
+ return elapsed() * 1000000;
+}
+
/*!
Returns the number of milliseconds since this QElapsedTimer was last
started. Calling this function in a QElapsedTimer that was invalidated
diff --git a/src/corelib/tools/qelapsedtimer_mac.cpp b/src/corelib/tools/qelapsedtimer_mac.cpp
index 8fceb49..e51f8b3 100644
--- a/src/corelib/tools/qelapsedtimer_mac.cpp
+++ b/src/corelib/tools/qelapsedtimer_mac.cpp
@@ -97,6 +97,12 @@ qint64 QElapsedTimer::restart()
return absoluteToMSecs(t1 - old);
}
+qint64 QElapsedTimer::nsecsElapsed() const
+{
+ uint64_t cpu_time = mach_absolute_time();
+ return absoluteToNSecs(cpu_time - t1);
+}
+
qint64 QElapsedTimer::elapsed() const
{
uint64_t cpu_time = mach_absolute_time();
diff --git a/src/corelib/tools/qelapsedtimer_symbian.cpp b/src/corelib/tools/qelapsedtimer_symbian.cpp
index 038b102..7cd5f1e 100644
--- a/src/corelib/tools/qelapsedtimer_symbian.cpp
+++ b/src/corelib/tools/qelapsedtimer_symbian.cpp
@@ -64,11 +64,6 @@ static quint64 getMicrosecondFromTick()
return nanokernel_tick_period * (val | (quint64(highdword) << 32));
}
-static quint64 getMillisecondFromTick()
-{
- return getMicrosecondFromTick() / 1000;
-}
-
timeval qt_gettime()
{
timeval tv;
@@ -91,36 +86,41 @@ bool QElapsedTimer::isMonotonic()
void QElapsedTimer::start()
{
- t1 = getMillisecondFromTick();
+ t1 = getMicrosecondFromTick();
t2 = 0;
}
qint64 QElapsedTimer::restart()
{
qint64 oldt1 = t1;
- t1 = getMillisecondFromTick();
+ t1 = getMicrosecondFromTick();
t2 = 0;
return t1 - oldt1;
}
+qint64 QElapsedTimer::nsecsElapsed() const
+{
+ return (getMicrosecondFromTick() - t1) * 1000;
+}
+
qint64 QElapsedTimer::elapsed() const
{
- return getMillisecondFromTick() - t1;
+ return (getMicrosecondFromTick() - t1) / 1000;
}
qint64 QElapsedTimer::msecsSinceReference() const
{
- return t1;
+ return t1 / 1000;
}
qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const
{
- return other.t1 - t1;
+ return (other.t1 - t1) / 1000;
}
qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const
{
- return msecsTo(other) / 1000;
+ return msecsTo(other) / 1000000;
}
bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2)
diff --git a/src/corelib/tools/qelapsedtimer_unix.cpp b/src/corelib/tools/qelapsedtimer_unix.cpp
index 633fa00..7407e1b 100644
--- a/src/corelib/tools/qelapsedtimer_unix.cpp
+++ b/src/corelib/tools/qelapsedtimer_unix.cpp
@@ -167,6 +167,17 @@ qint64 QElapsedTimer::restart()
return elapsedAndRestart(t1, t2, &t1, &t2);
}
+qint64 QElapsedTimer::nsecsElapsed() const
+{
+ qint64 sec, frac;
+ do_gettime(&sec, &frac);
+ sec = sec - t1;
+ frac = frac - t2;
+ if (!monotonicClockAvailable)
+ frac *= 1000;
+ return sec * Q_INT64_C(1000000000) + frac;
+}
+
qint64 QElapsedTimer::elapsed() const
{
qint64 sec, frac;
diff --git a/src/corelib/tools/qelapsedtimer_win.cpp b/src/corelib/tools/qelapsedtimer_win.cpp
index c77acaa..3f4acc1 100644
--- a/src/corelib/tools/qelapsedtimer_win.cpp
+++ b/src/corelib/tools/qelapsedtimer_win.cpp
@@ -79,14 +79,14 @@ static void resolveLibs()
done = true;
}
-static inline qint64 ticksToMilliseconds(qint64 ticks)
+static inline qint64 ticksToNanoseconds(qint64 ticks)
{
if (counterFrequency > 0) {
// QueryPerformanceCounter uses an arbitrary frequency
- return ticks * 1000 / counterFrequency;
+ return ticks * 1000000000 / counterFrequency;
} else {
// GetTickCount(64) return milliseconds
- return ticks;
+ return ticks * 1000000;
}
}
@@ -144,24 +144,30 @@ qint64 QElapsedTimer::restart()
qint64 oldt1 = t1;
t1 = getTickCount();
t2 = 0;
- return ticksToMilliseconds(t1 - oldt1);
+ return ticksToNanoseconds(t1 - oldt1) / 1000000;
+}
+
+qint64 QElapsedTimer::nsecsElapsed() const
+{
+ qint64 elapsed = getTickCount() - t1;
+ return ticksToNanoseconds(elapsed);
}
qint64 QElapsedTimer::elapsed() const
{
qint64 elapsed = getTickCount() - t1;
- return ticksToMilliseconds(elapsed);
+ return ticksToNanoseconds(elapsed) / 1000000;
}
qint64 QElapsedTimer::msecsSinceReference() const
{
- return ticksToMilliseconds(t1);
+ return ticksToNanoseconds(t1) / 1000000;
}
qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const
{
qint64 difference = other.t1 - t1;
- return ticksToMilliseconds(difference);
+ return ticksToNanoseconds(difference) / 1000000;
}
qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const
diff --git a/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp b/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp
index 87df57d..bc61f52 100644
--- a/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp
+++ b/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp
@@ -122,11 +122,13 @@ void tst_QElapsedTimer::basics()
quint64 value1 = t1.msecsSinceReference();
qDebug() << value1 << t1;
+ qint64 nsecs = t1.nsecsElapsed();
qint64 elapsed = t1.restart();
QVERIFY(elapsed < minResolution);
+ QVERIFY(nsecs / 1000000 < minResolution);
quint64 value2 = t1.msecsSinceReference();
- qDebug() << value2 << t1 << elapsed;
+ qDebug() << value2 << t1 << elapsed << nsecs;
// in theory, elapsed == value2 - value1
// However, since QElapsedTimer keeps internally the full resolution,
@@ -150,7 +152,10 @@ void tst_QElapsedTimer::elapsed()
// don't check: t1.secsTo(t2)
// QVERIFY(t1 - t2 < 0);
+ QVERIFY(t1.nsecsElapsed() > 0);
QVERIFY(t1.elapsed() > 0);
+ // the number of elapsed nanoseconds and milliseconds should match
+ QVERIFY(t1.nsecsElapsed() - t1.elapsed() * 1000000 < 1000000);
QVERIFY(t1.hasExpired(minResolution));
QVERIFY(!t1.hasExpired(8*minResolution));
QVERIFY(!t2.hasExpired(minResolution));