http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=211457
你需要多精确?
GetTickCount可以到18-20ms进度
timeGetTime可以到1ms精度
……
当然这些都不是C or c++ 标准支持的。
那么就要祭出最牛奔的方法,
直接读取CPU开机以来执行的机器周期数,
一条汇编指令:RDTSC (就是 ReaD TimeStamp Count)
精度可以达到ns级别。(准确地说精度是1 / 你的CPU的时钟频率,这也是极限)
long HighStart,LowStart,HighEnd,LowEnd;
long numhigh,numlow;
__asm
{
RDTSC
mov HighStart, edx
mov LowStart, eax
// put your time-consuming code here ……
RDTSC
mov HighEnd, edx
mov LowEnd, eax
//获取两次计数器值得差
sub eax, LowStart
cmp eax, 0
jg L1
neg eax
jmp L2
L1: mov numlow, eax
L2: sbb edx, HighStart
mov numhigh, edx
}
__int64 timer =(numhigh<<32) + numlow; //得出最终结果
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=562995
<script type="text/javascript">document.write(" ");</script>
去掉下面的就没问题了,但要怎样
put my time-consuming code 呢?
int x=100000;
while(x>0)
x--;
#include <iostream.h>
void main()
{
long HighStart,LowStart,HighEnd,LowEnd;
long numhigh,numlow;
__asm
{
RDTSC
mov HighStart, edx
mov LowStart, eax
// put your time-consuming code here ……
int x=100000;
while(x>0)
x--;
RDTSC
mov HighEnd, edx
mov LowEnd, eax
//获取两次计数器值得差
sub eax, LowStart
cmp eax, 0
jg L1
neg eax
jmp L2
L1: mov numlow, eax
L2: sbb edx, HighStart
mov numhigh, edx
}
}
int timer =(numhigh<<32) + numlow; //得出最终结果
cout<<timer<<endl;
}
int x=100000;
while(x>0)
x--;
-----------------------------
------------------------
__asm
{
//这里是不是只能放汇编代码呀?
}
》》put my time-consuming code 呢?
put my time-consuming code是告诉你那个位置是放我们的代码的地方
》》__asm
》》{
》》//这里是不是只能放汇编代码呀?
》》}
是的。这是内联汇编指令。