Posted on 2006-02-04 16:40
Tommy Liang 阅读(240)
评论(0) 编辑 收藏 引用
inline unsigned __int64 GetCycleCount(void)
{
_asm _emit 0x0F
_asm _emit 0x31
} dasm如下:
: inline unsigned __int64 GetCycleCount(void)
7: {
00401070 55 push ebp
00401071 8B EC mov ebp,esp
00401073 83 EC 40 sub esp,40h
00401076 53 push ebx
00401077 56 push esi
00401078 57 push edi
00401079 8D 7D C0 lea edi,[ebp-40h]
0040107C B9 10 00 00 00 mov ecx,10h
00401081 B8 CC CC CC CC mov eax,0CCCCCCCCh
00401086 F3 AB rep stos dword ptr [edi]
8: _asm _emit 0x0F
00401088 0F 31 rdtsc
10: }
0040108A 5F pop edi
0040108B 5E pop esi
0040108C 5B pop ebx
0040108D 83 C4 40 add esp,40h
00401090 3B EC cmp ebp,esp
00401092 E8 19 00 00 00 call __chkesp (004010b0)
00401097 8B E5 mov esp,ebp
00401099 5D pop ebp
0040109A C3 ret
关键就是那个RDTSC指令,即 Read Time Stamp Counter, 结果会保存在EDX:EAX寄存器对中。
Intel的文档是这样说的:
With the Pentium processor, Intel added an additional instruction called RDTSC (Read Time Stamp
Counter). This gives software direct access to the number of clock counts the processor has
experienced since its last power-on or hardware reset. With contemporary clock rates of, for
example, 3.06 Ghz, that results in a timing period of only 0.326 nanoseconds.