主题:[讨论]C++ 计算CPU执行周期
#include <iostream>
#include <windows.h>
#include <time.h>
#include <vector>
using namespace std;
int main()
{
long hStart, lStart, hEnd, lEnd;
long hCnt, lCnt;
__asm
{
RDTSC
mov hStart,edx
mov lStsrt,eax
}
__asm
{
RDTSC
mov hEnd,edx
mov lend,eax
sub eax,lStart
cmp eax,0
jg L1
neg eax
jmp L2
L1: mov lCnt,eax
L2: sub edx,hStart
mov hCnt,edx
}
unsigned long timer=(hCnt<<16)+lCnt;
cout<<"Time: "<<timer<<endl;
system("pause");
return 0;
}
此程序调不通~
我用的32位机器,VS2010~谢谢各位大虾~
#include <windows.h>
#include <time.h>
#include <vector>
using namespace std;
int main()
{
long hStart, lStart, hEnd, lEnd;
long hCnt, lCnt;
__asm
{
RDTSC
mov hStart,edx
mov lStsrt,eax
}
__asm
{
RDTSC
mov hEnd,edx
mov lend,eax
sub eax,lStart
cmp eax,0
jg L1
neg eax
jmp L2
L1: mov lCnt,eax
L2: sub edx,hStart
mov hCnt,edx
}
unsigned long timer=(hCnt<<16)+lCnt;
cout<<"Time: "<<timer<<endl;
system("pause");
return 0;
}
此程序调不通~
我用的32位机器,VS2010~谢谢各位大虾~