質問
GetTickCount()
コードセグメントが次のように実行されるのにかかる時間を測定できるもの:
uint start = GetTickCount();
// do something..
uint timeItTook = GetTickCount() - start;
提案はありますか
解決
QTime
はどうですか?プラットフォームによっては、1ミリ秒の精度が必要です。コードは次のようになります。
QTime myTimer;
myTimer.start();
// do something..
int nMilliseconds = myTimer.elapsed();
他のヒント
QElapsedTimer
それがクラスがそもそも存在する理由だからです。 Qt 4.7で導入されました。また、システムの時刻の変更の影響を受けないことに注意してください。
使用例:
#include <QDebug>
#include <QElapsedTimer>
...
...
QElapsedTimer timer;
timer.start();
slowOperation(); // we want to measure the time of this slowOperation()
qDebug() << timer.elapsed();
最初の回答が受け入れられたとしても、回答を読んだ残りの人々は、 sivabudh
の提案を考慮する必要があります。
QElapsedTimer
は、時間の計算にも使用できます。ナノ秒単位。
コード例:
QElapsedTimer timer;
qint64 nanoSec;
timer.start();
//something happens here
nanoSec = timer.nsecsElapsed();
//printing the result(nanoSec)
//something else happening here
timer.restart();
//some other operation
nanoSec = timer.nsecsElapsed();
QElapsedTimer
を使用する場合、このクラスのオーバーヘッドを考慮する必要があります。
たとえば、私のマシンで次のコードを実行します:
static qint64 time = 0;
static int count = 0;
QElapsedTimer et;
et.start();
time += et.nsecsElapsed();
if (++count % 10000 == 0)
qDebug() << "timing:" << (time / count) << "ns/call";
次の出力が表示されます:
timing: 90 ns/call
timing: 89 ns/call
...
これを自分で測定し、タイミングのオーバーヘッドを尊重する必要があります。
以前の回答を使い切って、ここにすべてを行うマクロがあります。
#include <QDebug>
#include <QElapsedTimer>
#define CONCAT_(x,y) x##y
#define CONCAT(x,y) CONCAT_(x,y)
#define CHECKTIME(x) \
QElapsedTimer CONCAT(sb_, __LINE__); \
CONCAT(sb_, __LINE__).start(); \
x \
qDebug() << __FUNCTION__ << ":" << __LINE__ << " Elapsed time: " << CONCAT(sb_, __LINE__).elapsed() << " ms.";
そして、次のように簡単に使用できます:
CHECKTIME(
// any code
for (int i=0; i<1000; i++)
{
timeConsumingFunc();
}
)
出力:
onSpeedChanged:102経過時間:2ミリ秒。
所属していません StackOverflow