I have the following code that needs to be ported to Visual Studio 2008. It computes how many cycles the processor completes in a microsecond in order to compute the processor speed:
typedef unsigned long long TProcessorClockTicks;
inline TProcessorClockTicks readProcessorTimeStamp()
{
# if defined(_ARCH_linux_24_i86) || \
defined(_ARCH_linux_26_i86) || \
defined(_ARCH_cygwin) || \
defined(_ARCH_freebsd_61_i86)
TProcessorClockTicks x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x / 1000;
# elif defined(_ARCH_sol2)
return (TProcessorClockTicks) ::gethrtime();
# else
# error Must define _ARCH_??? preprocessor macro to build \
this code!
# endif
}
typedef unsigned long long TProcMHz;
inline
TProcMHz
measureProcessorSpeedMHz()
{
// Get the number of the processor clock cycles in a microsecond which should
// be the processor clock speed in MHz.
::usleep(1); // Put a sleep before to ensure accurate measurement.
TProcessorClockTicks before = readProcessorTimeStamp();
::usleep(1);
TProcessorClockTicks after = readProcessorTimeStamp();
return (TProcMHz) (after - before);
}
What is the replacement for the readProcessorTimeStamp() body?
TProcessorClockTicks x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x / 1000;
Your code assumes that
::usleep(1)sleeps for exactly one microsecond. I don’t know if that’s a good assumption on a Linux box. It’s certainly not a good assumption in Windows. At least, not in user mode.Under Windows, there aren’t any standard microsecond-level timers. The best you can do with the standard API calls is one millisecond, and even that’s variable. In order to get a reasonable estimate of the clock speed, you’ll need to get the elapsed time as well as the number of ticks elapsed.
Or you can query the registry for it. See http://www.codeproject.com/KB/system/Processor_Speed.aspx for some options.