[急]没有看出汇编速度和大小的明显优势

[复制链接]
查看11 | 回复1 | 2010-9-16 11:08:20 | 显示全部楼层 |阅读模式
我分别用汇编和C++来递归实现冒泡排序
排20个完全逆序的数据,重复100000次
可是汇编运行时间为 220ms左右, C++运行时间为116ms 为何C++会更快呢
查看时间:
1.汇编通过调用DOS 2ch中断
2. C++ 通过前后输出系统时间相减
冒泡代码如下:
1.汇编
mov cx, 20;数组长度
dec cx
bubblesort proc near
push ax
push bx
push cx
mov bx, 0
_loop:

mov ax, array[bx]
cmp ax, array[bx+2]
jle continue
xchg ax, array[bx+2]
mov array[bx], ax
continue:
add bx, 2
loop _loop
pop cx
dec cx

cmp cx, 0
jz exit
call bubblesort
exit :
pop bx
pop ax
ret
2.C
void sortArray(int array[], int n, int length){

if (n == length) return;

int midint;

for (int j = length - 1; j > n; j--)

{



if (array[j] > array[j - 1])

{

midint = array[j];

array[j] = array[j - 1];

array[j - 1] = midint;

}

回复

使用道具 举报

千问 | 2010-9-16 11:08:20 | 显示全部楼层
现在的编译器已经可以对代码优化做得很好了,来看一下你上面这段c代码在release下的汇编代码push
ebpmov
ebp, espmov
edx, nmov
eax, lengthcmp
edx, eaxje L025lea
ecx, dword ptr [eax-1]cmp
ecx, edxjle L025mov
eax, Arraypush
esimov
eax, Arraysub
ecx, edxL015:mov
edx, dwor
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行