tommy

It's hard to tell the world we live in is either a reality or a dream
posts - 52, comments - 17, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
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.

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理