我采用在线方式来衡量SSE性能。
#ifndef __TIMER_H__
#define __TIMER_H__
#pragma warning (push)
#pragma warning (disable : 4035) // disable no return value warning
__forceinline unsigned int GetPentiumTimer()
{
__asm
{
xor eax,eax // VC won't realize that eax is modified w/out this
// instruction to modify the val.
// Problem shows up in release mode builds
_emit 0x0F // Pentium high-freq counter to edx;eax
_emit 0x31 // only care about low 32 bits in eax
xor edx,edx // so VC gets that edx is modified
}
}
#pragma warning (pop)
#endif
我对我的 Pentium D E2200 CPU 进行了测量,结果很好(显示对齐的 SSE 指令更快)。 但是在我的 i3 CPU 上,70% 的测试中未对齐的指令更快。
你们认为这个时钟滴答测量方法不适用于 i3 CPU 吗?
RDTSC
指令。另外,为什么你不关心高32位,并且你应该使用__declspec(naked)
或者更好的方式返回值。此外,我想使用QueryPerformanceCounter
或类似的函数(注意频率缩放/多核处理器等问题)。 - user786653QueryPerformanceCounter
。 - AJG85rdtsc
,请使用__rdtsc()
内置函数。如何获取 CPU 周期计数? - Peter Cordes