공룡호가 사는 세상 이야기

어떤 명령어의 수행시간을 측정하고 싶을때가 있다.
이때 유용하게 사용할수 있는 함수가 GetTickCount()이다.

GetTickCount()함수는 시스템이 시작 된 후 얼마의 시간이 경과했는지를 반환한다. 단위는 밀리세컨드 단위이다. 참고로 경과시간은 DWORD(32비트 값)이므로 최대 49.7일이 지나면 다시 0으로 된다고 한다.

The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.

DWORD GetTickCount(void);

Parameters : This function has no parameters.
Return values : The return value is the number of milliseconds that have elapsed since the system was started.


#include <iostream>
#include <windows.h> //GetTickCount()함수 이용을 위해 추가한다.

using namespace std;

void main()
{
long startTime = GetTickCount(); //현재 시각을 저장한다.(시작지점)

for(int i=0; i<100000; i++) //시간 딜레이를 주기 위해 i값을 출력한다.
cout<<i<<endl;; //여기서는 테스트 하기 위한 명령어라고 보면 된다.

long endTime = GetTickCount(); //현재 시각을 저장한다.(종료지점)
long tickDiff = endTime - startTime; //수행시간 = 종료시각 - 시작시각
long secDiff = tickDiff / 1000 ; //이건 초단위로도 나타내기 위한 것.

cout<<"Start Time : "<<startTime<<"ms"<<endl; //시작시각
cout<<"End Time : "<<endTime<<"ms"<<endl; //종료시각
cout<<"Tick difference : "<<tickDiff<<"ms"<<endl; //밀리세컨드 단위
cout<<"Second difference : "<<secDiff<<"s"<<endl; //초단위
}

'프로그래밍' 카테고리의 다른 글

ACM 10018 Reverse And Add  (0) 2006.01.16
ACM 10127 ONES  (0) 2006.01.16
chaos in FOR loop  (0) 2006.01.13
ACM 10110 Light, More Light  (0) 2006.01.13
ACM 100 3n+1 Problem  (2) 2006.01.10