'QueryPerformanceCounter'에 해당되는 글 1건

  1. 2020.02.23 [C/C++] 경과시간 측정 함수 QueryPerformanceCounter()

QueryPerformanceCounter(LARGE_INTEGER * lpPerformanceCount)

는 1 ㎲ 이하로 시간을 체크 할 수 있는 고성능 시간 체크 함수다. 

(https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms644904(v=vs.85).aspx)

 

※ LARGE_INTEGER union (https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa383713(v=vs.85).aspx)

 

사용방법은 간단하다 

 

QueryPerformanceCounter(&nowCounter); 

// nowCounter 값을 현재 프로세서에서의 PerformanceCounter 주파수 값으로 나누면 현재 시간이 된다.

// PerformanceCounter 주파수 값은 

// QueryPerformanceFrequency(LARGE_INTEGER * lpFrequency) 로 가져 올 수 있다.

 

아래는 QueryPerformanceCounter() 로 배열 합 연산의 경과시간을 측정 하는 예제이다.

(파일첨부)

 

 

//------------------------------ QueryPerformance_Test.cpp --------------------------------

 

#include <stdio.h>

#include <windows.h>

 

const int ARRAY_SIZE = 2073600; // 1920 * 1080

const int REPEAT_COUNT = 1000;

 

int main()

{

printf_s("QueryPerformance Function Test Program.\n");

printf_s("- Array Add(2073600 size).\n\n");

 

LARGE_INTEGER frequency;

LARGE_INTEGER beginTime;

LARGE_INTEGER endTime;

 

// variable allocation.

int *srcData_1 = new int[ARRAY_SIZE];

int *srcData_2 = new int[ARRAY_SIZE];

int *dstData = new int[ARRAY_SIZE];

 

// initialize variable

for(int i = 0; i < ARRAY_SIZE; i++)

{

srcData_1[i] = i;

srcData_2[i] = ARRAY_SIZE - i + 1;

dstData[i] = 0;

}

 

// get the frequency of the performance counter 

QueryPerformanceFrequency( &frequency );

printf_s("QueryPerformanceFrequency = %d\n", frequency.QuadPart);

 

// check start time.

QueryPerformanceCounter(&beginTime);

 

// repeated a 1000 times.

for(int repeatIndex = 0; repeatIndex < REPEAT_COUNT; repeatIndex++)

{

// array add

for(int i = 0; i < ARRAY_SIZE; i++)

{

dstData[i] = srcData_1[i] + srcData_2[i];

}

}

 

// check end time.

QueryPerformanceCounter(&endTime);

 

// calculate the elapsed time. 

double elapsedTimeMS = (static_cast<double>(endTime.QuadPart - beginTime.QuadPart) / frequency.QuadPart); // omitted *1000(sec -> ms) & /1000(repeted a 1000 times)

printf_s("A 1000 times the average elapsed time(ms) = %.2f ms\n", elapsedTimeMS); // print format : x.xx 

 

// delete variable.

delete [] srcData_1;

delete [] srcData_2;

delete [] dstData;

 

    return 0;

 

}

 

Posted by 소프트장
,