Programing

Qt에서 경과 시간 가져 오기

lottogame 2020. 10. 28. 07:39
반응형

Qt에서 경과 시간 가져 오기


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나노초 단위로 시간을 계산하는 데 사용할 수도 있습니다.
코드 예 :

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.

참고 URL : https://stackoverflow.com/questions/244646/get-elapsed-time-in-qt

반응형