头一次实用化汇编嵌入,测试结果运行时间8:18!!!!!!!!!同样的测试数据,同样的算法,基本上就是翻译来的。太爽了,本人总结,汇编辰哥超级改装版把基本上所有的操作都移到寄存器里,同时简化了N多无用代码,速度自然快很多,但也快的太多了...不过调试的过程异常痛苦@@@@吐血...连蒙带撞也算是挺过来了,哈哈,这就是成就感!
--2010-1-25--
原版:
- void shell(int src[],int l,int r){
- register int cmps;
- int ih,il,ir;
- for(ih=1;ih<r/9;ih=ih*3+1);
- for(;ih>0;ih/=3)
- for(il=ih-1;il<r;il++)
- {
- ir=il;
- cmps=src[ir];
- while(ir>ih-1&&cmps<src[ir-ih])
- {
- src[ir]=src[ir-ih];ir-=ih;
- }
- src[ir]=cmps;
- }
- }
汇编辰哥超级改装版:
- void shell(int src[],int l,int r){
- int ih;
- r++;
- for(ih=1;ih<(r-l)/9;ih=ih*3+1);
- //eax,ih
- //ebx,il
- //ecx,ir
- //edx,cmps
- _asm{
- push eax
- push ebx
- push ecx
- push edx
- push esi
- push edi;貌似这堆进栈用处不大哎
- mov edi,src
- mov eax,dword ptr [ih]
- LIH:
- cmp eax,0
- jna EXIH
- mov ebx,eax
- dec ebx
- LLH:
- cmp ebx,dword ptr [r]
- jnb EXLLH
- mov ecx,ebx
- mov edx,dword ptr [edi+ecx*4]
- LCMP:
- mov esi,eax
- dec esi
- cmp ecx,esi
- jna EXCMP
- push ecx
- sub ecx,eax
- cmp edx,dword ptr [edi+ecx*4]
- pop ecx
- jnb EXCMP
- push ebx
- push ecx
- sub ecx,eax
- mov ebx,dword ptr [edi+ecx*4]
- pop ecx
- mov dword ptr [edi+ecx*4],ebx
- pop ebx
- sub ecx,eax
- jmp LCMP
- EXCMP:
- mov dword ptr [edi+ecx*4],edx
- inc ebx
- jmp LLH
- EXLLH:
- push ecx
- mov ecx,3
- push edx
- cdq
- idiv ecx
- pop edx
- pop ecx
- jmp LIH
- EXIH:
- pop edi
- pop esi
- pop edx
- pop ecx
- pop ebx
- pop eax
- }
- }