gangslab
3/16/2015 - 6:51 AM

C# 에서 QueryPerformanceCounter 사용하기

C# 에서 QueryPerformanceCounter 사용하기

using System;
using System.Runtime.InteropServices;

namespace csConPerfCounter
{
	class Class1
	{
		[DllImport("kernel32.dll")]
		extern static short QueryPerformanceCounter(ref long x);
		[DllImport("kernel32.dll")]
		extern static short QueryPerformanceFrequency(ref long x);

		static void Main(string[] args)
		{
			long ctr1 = 0, ctr2 = 0, freq = 0;
			int acc = 0, i = 0;
			if (QueryPerformanceCounter(ref ctr1)!=0)	// Begin timing.
			{
				for (i=0; i<100; i++) acc++;		// Code being timed.
				QueryPerformanceCounter(ref ctr2);	// Finish timing.
				Console.WriteLine("Start Value: " + ctr1);
				Console.WriteLine("End Value: " + ctr2);
				QueryPerformanceFrequency(ref freq);
				Console.WriteLine("QueryPerformanceCounter minimum resolution: 1/" + freq + " seconds.");
				Console.WriteLine("100 Increment time: " + (ctr2 - ctr1) * 1.0 / freq + " seconds.");
			}
			else
			   Console.WriteLine("High-resolution counter not supported.");

			// Make the console window wait.

			Console.WriteLine();
			Console.Write("Press Enter to finish ... ");
			Console.Read();
		}
	}
}