/* QueryPerformanceTimerTest01
*
* By Philip Hagen
* phil [AT] evilpixel.org
* 20 August 2006
*
* This console application tests the QueryPerformanceCounter() and QueryPerformanceFrequency()
* Windows API methods.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; // for DllImport attribute
namespace PhilipHagen.QueryPerformanceTimerTest01
{
class QueryPerformanceTimerTestProgram
{
///
/// Number of seconds to run this test for.
///
private const int SecondsToRun = 3;
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("*** QueryPerformanceTimerTest01 ***");
Console.WriteLine("By Philip Hagen, 20 August 2006");
Console.WriteLine();
// Query the frequency of the high performance counter (in ticks per second)
// and determine if this system supports high-performance timers.
long frequency = 0;
bool supported = QueryPerformanceFrequency(out frequency);
// If high-performance timers are not supported, inform the user and exit..
if (!supported)
{
Console.WriteLine("Your system does not support high performance timers.");
// Wait for keypress from user before terminating.
Console.WriteLine();
Console.WriteLine("Press a key to finish...");
Console.ReadKey(true);
return;
}
Console.WriteLine(string.Format("Timer frequency = {0} updates per second", frequency));
Console.WriteLine("Running test for " + SecondsToRun + " seconds...");
Console.WriteLine();
DateTime startTime = DateTime.Now;
TimeSpan timeToRunFor = new TimeSpan(0, 0, SecondsToRun);
DateTime endTime = startTime.Add(timeToRunFor);
// Stores the number of ticks in the previous frame.
long ticksLast = 0;
// Stores the number of ticks now.
long ticks = 0;
// Stores the total number of ticks.
long totalTickCount = 0;
// Stores the number of bad ticks.
long badTickCount = 0;
QueryPerformanceCounter(out ticks);
// Keep looping until the desired time has passed.
while (DateTime.Now < endTime)
{
++totalTickCount;
ticksLast = ticks;
// Get current counter value.
QueryPerformanceCounter(out ticks);
if (ticks > ticksLast)
{
// Ticks are larger than previous; as expected!
Console.ForegroundColor = ConsoleColor.Green;
}
else
{
// Ticks are less than previous!!!
Console.ForegroundColor = ConsoleColor.Red;
++badTickCount;
}
Console.WriteLine(string.Format("Ticks = {0} (difference = {1:0})", ticks, ticks - ticksLast));
}
Console.ForegroundColor = ConsoleColor.Gray;
// Print summary results.
Console.WriteLine();
Console.WriteLine(string.Format("Timer frequency was reported as {0} ticks per second", frequency));
string resultString = string.Format("In {0} seconds of testing, {1} of {2} measurements were bad ({3:0.00}%).",
SecondsToRun, badTickCount, totalTickCount, (float)badTickCount / (float)totalTickCount * 100.0f);
Console.WriteLine(resultString);
// Wait for keypress from user before terminating.
Console.WriteLine();
Console.WriteLine("Press a key to finish...");
Console.ReadKey(true);
}
///
/// Obtains the current high-performance timer ticks value.
///
/// Will be set to the current timer ticks.
/// True if the system contains a high-performance timer, false if it doesn't.
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);
///
/// Obtains the frequency of the system's high-performance timer. This value indicates the number
/// of times the high-performance timer increments itself every second.
///
/// Will be set to the frequency of the high-performance timer.
/// True if the system contains a high-performance timer, false if it doesn't.
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency);
}
}