0
我有以下需要移植到Visual Studio 2008的代碼。它計算處理器在一微秒內完成的計算處理器速度的週期數:如何將處理器速度的彙編代碼移植到Visual Studio 2008
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);
}
什麼是替代readProcessorTimeStamp()正文?
TProcessorClockTicks x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x/1000;
我不認爲這是行得通的。此外,:: gethrtime返回納秒,而不是時鐘滴答聲,因此無論時鐘速度如何,代碼大小寫都會返回相同的結果。 – TJD
你確實意識到這種方法不準確?睡眠/睡眠不會完全按照指定的時間量睡,只是一個近似值。如果您想在Windows上獲得準確的信息,請考慮使用WMI Win32_Processor類(請參閱http://msdn.microsoft.com/zh-cn/library/aa394373(VS.85).aspx)。 –
目前我並不太在意它的準確程度,只是希望它在球場停放並移植。 – WilliamKF