我用c写了一个插入排序法 
然后我用汇编也写了一个插入排序法
因为我的汇编差 无法提高效率 我的汇编写发完全按高级语言的思路写出来的
之后反汇编c写的插入排序法 发现我的汇编代码和vc6.0生成的汇编代码完全一样可是 为什么我的汇编代码比c代码慢那么多 几十倍?
代码如下:请指教
#include "stdafx.h"
#include <windows.h>void insertsort(int *lparray,int nlength)
{
#define ASMSORT
#ifdef ASMSORT

typedef int SYMBOL; __asm
{ mov esi,lparray  //保存 lparray的首地址
mov ecx,0  //初始化 i =0;

continue0: inc ecx  //i++;
mov edx,ecx  //j=i;
inc edx  
mov ebx,dword ptr [esi + type SYMBOL * ecx] //tmp = *(lparray + i);
continue1:
dec edx  //j--;
cmp ebx,dword ptr [esi + type SYMBOL * edx - type SYMBOL]/////////////////////////////////////
jg exit2    //if(tmp < *(lparray + j -1))
mov eax,dword ptr [esi + type SYMBOL * edx - type SYMBOL]
MOV dword ptr [esi + type SYMBOL * edx],eax    //*(lparray + j) = *(lparray + j -1);
   /////////////////////////////////////
cmp edx,0x0    //for(j=i; j>0; j--)   
jg continue1    //if(j>0)goto continue1 (j--)
exit2:
mov dword ptr [esi + type SYMBOL * edx],ebx //*(lparray + j) = tmp;
cmp ecx,nlength //for(i=1; i<nlength; i++)
jl continue0 //if(i < nlength)goto continue0 (i++) }
#else
//*#########################################################################################
int i,j;
int tmp = 0;

for(i=1; i<nlength; i++)
{
tmp = *(lparray + i);

for(j=i; j>0; j--) 
{
if(tmp < *(lparray + j -1))
*(lparray + j) = *(lparray + j -1);
else
break;
}

*(lparray + j) = tmp;
}
//#########################################################################################*/
#endif
}int main(int count, char* commands[])
{

const int n = 8000;
int number[n] = {5,9,8,6,7,1,3,0,2,4,5,9,8,6,7,1,3,0,2,4}; unsigned long timeed = GetTickCount();
insertsort(*&number,n);
printf("%d\n",GetTickCount() - timeed); printf("\n");
// for(int i=0; i<n; i++)
// printf("%d\n",number[i]); return 0;
}

解决方案 »

  1.   

    晕 怎么贴上来边乱了  __asm
    {  mov esi,lparray  //保存 lparray的首地址
     mov ecx,0  //初始化 i =0;

    continue0:inc ecx  //i++;
     mov edx,ecx  //j=i;
     inc edx
              //tmp = *(lparray + i);  
     mov ebx,dword ptr [esi + type SYMBOL * ecx]
    continue1:
    dec edx  //j--;
    cmp ebx,dword ptr [esi + type SYMBOL * edx - type       SYMBOL]
    jg exit2 //if(tmp < *(lparray + j -1))
    mov eax,dword ptr [esi + type SYMBOL * edx - type SYMBOL]
    MOV dword ptr [esi + type SYMBOL * edx],eax //*(lparray + j) = *(lparray + j -1);
    cmp edx,0x0 //for(j=i; j>0; j--)   
    jg continue1 //if(j>0)goto continue1 (j--)
    exit2:
    dword ptr [esi + type SYMBOL * edx],ebx //*(lparray + j) = tmp;
    cmp ecx,nlength //for(i=1; i<nlength; i++)
    jl continue0 //if(i < nlength)goto continue0 (i++)
    }
      

  2.   

    反汇编c写的插入排序法 不一定就和VC翻译的汇编一样~~
      

  3.   

    呵呵,描述算法,用asm不是自己找事吗,你可以这样做,debug c 的code时,反汇编,把asm提取出,稍微精简些,算法类asm和c没什么区别速度上,看编译器的性能了,你可以试一下intel的fortran(编译c的~~~~~~),性能不错的
      

  4.   

    如果你对性能要求不是很高,建议用c写,然后选择一个比较好的编译器,如intel compiler 8.0
    如果对性能要求很高,建议先看intel的3本手册,要是对处理器结构不了解,写出来的汇编代码还没有编译器来高效。写
      

  5.   

    数据块的移动建议用memmove,不要用for(...)的方式