본문 바로가기
컴퓨터/C#

C# 시스템 시간 측정

by Luyin 2012. 11. 16.

PC가 측정할 수 있는 (최소의) 시간은 CPU 의 Frequency에 좌우 된다.

현재 사용하고 있는 PC는 3.4GHz 의 CPU를 장착하고 있다. 

 


이를 통해 PC가 측정할 수 있는 시간을 계산해보면, 

 1(초)/3,400,000(Hz) = 300ns

이 된다.


즉, 시간 측정의 정확도는 300ns 가 된다.


실제로 C# 프로그램을 통해 CPU Frequency와 시간 측정 정확도를 구해 보았다.

코드는 다음과 같다.


 C# CPU Frequency 측정, 시간 측정 정확도 구하는 Source Code

using System.Diagnostics;

 //나노시간을 측정하는 함수

// Stopwatch 클래스 사용 public static void DisplayTimerProperties() { // Display the timer frequency and resolution. if (Stopwatch.IsHighResolution) { Console.WriteLine("Operations timed using the system's high-resolution performance counter."); } else { Console.WriteLine("Operations timed using the DateTime class."); } long frequency = Stopwatch.Frequency; Console.WriteLine(" Timer frequency in ticks per second = {0}", frequency);

long nanosecPerTick = (1000L * 1000L * 1000L) / frequency;

Console.WriteLine(" Timer is accurate within {0} nanoseconds", nanosecPerTick);

}