This is the most accurate timing code I've seen for Windows:
Code:
/****************\
** SetupTiming *************************************************\
** **
** SetupTiming increases the waits for 1 second for other **
** tasks to finish, and increases the priority of the **
** solver. Since we are using a high preformace timers, **
** these steps help make the timing more accurate and **
** repeatable (there is still some variation from run to **
** run, but much less than if we don't do this). **
** **
** Note: under Windows, doing this really does make the **
** timings more consistant. Under Linux, there does **
** not appear to be anything I can do to improve **
** the consistancy of the timings. The timings **
** appear to change by 10% or more between runs. **
** **
\****************************************************************/
void SetupTiming (void)
{
// Only do this for Windows, it does not seem to help under Linux
#ifdef WIN32
// Get the frequency of the timer (for my system, it was a 0.279 usec a tick)
QueryPerformanceFrequency (&Frequency);
// If puzzle timing, wait 1 second before running the puzzle to allow
// other background threads to finish up. This provides better timings
if (timing_mode)
{
GETTIME (StartTime);
do { GETTIME (EndTime); }
while ((EndTime.QuadPart - StartTime.QuadPart) < Frequency.QuadPart);
}
// To make the timing more accurate, raise the priority of the program.
// This helps prevent other things from inturrupting the solving.
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
#endif
}
#ifdef WIN32
#include <windows.h> // Allow high preformance timing routines
#else
#include <time.h> // Allow high preformance timing routines
#include <stdlib.h> // For exit()
#endif
#include <stdio.h>
// For timing, use the high preformance windows timers
#ifdef WIN32
LARGE_INTEGER Frequency, StartTime, EndTime, SolveTime;
#define GETTIME(x) QueryPerformanceCounter (&x)
#endif
if (timing_mode == 2)
GETTIME (StartTime); // Get the current timer for puzzle timing
PuzSolCnt = Solver(num_search, use_subsets, buffer); // Solve the puzzle
if (timing_mode == 2)
GETTIME (EndTime); // Get ending time if timing puzzles
if (double_check) DoubleCheck(); // Verify the solution
SolvedCnt += PuzSolCnt; // Update solved count
DisplaySolution(); // Display the solution and timing
}
GETTIME (EndTime); // Get ending timer
DisplayTiming();
void DisplayTiming (void)
{
float fulltime;
#ifdef WIN32
fulltime = (float)(EndTime.QuadPart - StartTime.QuadPart) / float)Frequency.QuadPart;
if (fulltime > 0.95)
printf (" Time = %3.3f sec ", fulltime);
else if (fulltime > 0.00095)
printf (" Time = %3.3f msec ", fulltime * 1000);
else
printf (" Time = %3.3f usec ", fulltime * 1000000);
}
Although the author of the above notes that Windows 32 bit has a bug in it's timing routines that sometimes reports negative times, I've seen this report very fine timing resolutions. Getting the frequency of the system is key, I'm sure.
It gets confusing because the author has various timing modes, and code, spread out across the file.
Hopefully, the above disparate bits of code will give you some clues, however.