본문 바로가기

히나아빠의 C++

시간 정밀 측정

clock_gettime을 이용해서 정밀한 시간측정을 할 수 있다.

    

    #define PER_MICROSEC 1000

    #define PER_MILLISEC 1000000

    #define PER_SEC 1000000000


    const long long NANOS = 1000000000LL;

    static struct timespec startTS, endTS;

    static long long retDiff = 0;

    //시간측정 시작

    clock_gettime(CLOCK_MONOTONIC, &startTS);


    //이곳에 측정할 함수를 위치시킨다.


    //시간측정 종료

    clock_gettime(CLOCK_MONOTONIC, &endTS);


    retDiff = NANOS * (endTS.tv_sec-startTS.tv_sec) + (endTS.tv_nsec-startTS.tv_nsec);


    YT_Printf("Measured Time : %lld(us)\n", retDiff/PER_MICROSEC);

    YT_Printf("Measured Time : %lld(ms)\n", retDiff/PER_MILLISEC);

    YT_Printf("Measured Time : %lld(s)\n", retDiff/PER_SEC);

'히나아빠의 C++' 카테고리의 다른 글

long long 형식의 출력  (0) 2018.12.11
std::getline  (0) 2018.11.23
static_assert  (0) 2018.11.22