문제
QT에서 동등한 것을 찾고 있습니다 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
나노 초에서 시간을 계산하는 데 사용될 수도 있습니다.
Code example:
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 경과 시간 : 2ms.
제휴하지 않습니다 StackOverflow