我正在学汇编,想编程看看汇编是不是比c++快,就在devC++ 4.9.9.2里弄了这个程序,但是编译无法通过,程序如下:
#include <iostream>
#include <cstdlib>
#include <ctime>
//测试汇编和c++哪个快
using namespace std;
int main()
{
int y = 0;
clock_t begin,end;
double result[2];
long i=0,times;
int A = 100,B = 200,C= 300;
times = 10000000;
//c++的A=A+B+C
begin = clock();
for(i = 0;i <times;i++)
{
A = A+B+C;
}
end = clock();
result[0] = (double)(end-begin)/CLOCKS_PER_SEC;
//汇编的A=A+B+C
begin = clock();
for(i = 0;i <times;i++)
{
//以下是照书copy的.
asm{push ax
mov ax,A
add ax,B
add ax,C
mov A,ax
pop ax
}
/*如果改成下面这样(花括号改成小括号)
asm(push ax
mov ax,A
add ax,B
add ax,C
mov A,ax
pop ax
)
还是出错*/
}
end = clock();
result[1] = (double)(end-begin)/CLOCKS_PER_SEC;
cout < < "结果为: " < <endl < < "c++: " < <result[0] < < "secnods " < <endl