High Performance Timer

2005/07/12 11:38



Kernel32.dll의 기능을 직접 가져다 쓰는 타이머로서 0.0001ms 단위까지 정확한 시간 측정이 가능한 타이머입니다. 과거 데브피아에 올렸던 글과 소스코드를 다시 블로그에 포스트합니다..

사용 방법:
1. 아래 소스 코드를 별도의 클래스로 생성한다.

2. 타이머를 사용하는 소스 코드 상에서 HiPerfTimer MyTimer = new HiPerfTimer(); 로 인스턴스 하나를 생성한다.

3. MyTimer.Start(); 와 MyTimer.Stop(); 을 소스 코드에 삽입하고 이 두 명령줄 사이에 시간을 측정하고자 하는 내용의 코드를 삽입한다.

4. 컴파일하고 실행한다.


----------------------------------

using System;

using System.Runtime.InteropServices;

using System.ComponentModel;

using System.Threading;



namespace YourNameSpaceHere

{

internal class HiPerfTimer

{

[DllImport("Kernel32.dll")]

private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);



[DllImport("Kernel32.dll")]

private static extern bool QueryPerformanceFrequency(out long lpFrequency);



private long startTime, stopTime;

private long freq;



// Constructor

public HiPerfTimer()

{

startTime = 0;

stopTime = 0;



if (QueryPerformanceFrequency(out freq) == false)

{

// high-performance counter not supported

throw new Win32Exception();

}

}



// Start the timer

public void Start()

{

// lets do the waiting threads there work

Thread.Sleep(0);



QueryPerformanceCounter(out startTime);

}



// Stop the timer

public void Stop()

{

QueryPerformanceCounter(out stopTime);

}



// Returns the duration of the timer (in seconds)

public double Duration

{

get

{

return (double)(stopTime - startTime) / (double) freq;

}

}

}

}


, , , ,

2005/07/12 11:38 2005/07/12 11:38






이 글의 관련 글
Trackback Address:http://www.wayiam.com/blog/trackback/11