컴퓨터/C#

[C#] ns(nano seconds) 나노 초 단위의 시스템 성능 측정 C# 코드

Luyin 2013. 7. 24. 11:32

C#에서

DateTime을 활용하면 ms 단위의 시간 밖에 측정 할 수 없다.

하지만, StopWatch 의 CPU Ticks 값, CPU 주파수(System.Frequency) 을 이용하면 ns(나노 초 )단위로 시스템 성능을 측정할 수 있다.


예제 코드는 아래와 같다.



 StopWatch 를 이용한 ns 단위 시스템 성능 측정

using System; using System.Threading; using System.Diagnostics; //StopWatch; class MainApp { static void Main(string[] args) { long StartTime, EndTime; Stopwatch SystemPerformanceWatch = new Stopwatch(); SystemPerformanceWatch.Start(); StartTime = SystemPerformanceWatch.ElapsedTicks; //성능 측정 코드 삽입 부분 Thread.Sleep(1000); EndTime = SystemPerformanceWatch.ElapsedTicks; Console.WriteLine("연산소모 시간 {0}", String.Format("{0:#,###.###} ns", (1000 * 1000 * 1000 * (EndTime - StartTime) / Stopwatch.Frequency))); } }