inline assembly vs. C + + implementation of bubble sorting, fast sorting algorithm sorting 500W data comparison

Source: Internet
Author: User

Embedded assembly is Microsoft in order to improve the direct operation of the program hardware capabilities, as well as the implementation of large-task program efficiency improvement, and embedded in the VC, he does not rely on the assembler Assembler code assembly, these embedded assembly code is implemented inside the C compiler, you can make the program as if self-trapped in the Assembly state. This means that if you are using inline ASM for assembly in A/C + + program, then you are doomed to cross-platform, and for those who do not have a compilation basis, they need to learn the Wang Shuang 8086 assembly program. , because sometimes C + + programmers have to understand these things, or you'll never know what the compiler did for your function. What else do you need to optimize, whatever the purpose I feel as a C + + programmer needs to understand.

A big brother around me told me that using the assembly is not necessarily faster than C + + I agree. Therefore, the efficiency of the program is not only the level of the operation of the decision, but more of the code of the program writer's quality decision.

The inline assembly is passed in C + +

The results of the actual discovery of 500W data are as follows:

Algorithm name inline assembly algorithm time C + + algorithm time

Bubble sort 5W Data slow dying 5W data slow to death

Quick sort 600ms about 500ms around

------------------Why there is a fast sorting algorithm, the results of the assembly is not a C + + efficiency is high, because I write the inline assembly is not automatically generated by the compiler high efficiency.

That is, the code is not high-quality reasons. ~~

_asm{...   } _asm ...  .

The introduction of assembly code, you can get the program from the Assembly state, MMX assembly instructions are widely used in processing media references.

The following is an inline assembler implementation function for the bubbling sort generation:

Time Complexity best  T (n) =o (n) ~o (n*n)  stability algorithm   

#define SIZE  void sortbubble (int arr[],int sizen) {int   *p;  p=arr-1;   &a[0]-1 __asm  {  mov   esi,p;  mov   Ecx,sizen;  _outter:  mov   edx,ecx;_inner:  cmp edx,ecx    ; remove equality jz  _exchange_nomov   eax,[esi+ecx*4 ];  ; In the function can not be directly through the array subscript get the passed array can only be addressed by the pointer mov   ebx,[esi+edx*4];  CMP   EAX,EBX;  JNB   _exchange_no;  mov   [esi+ecx*4],ebx;   ; Exchange of even elements mov   [esi+edx*4],eax;  _exchange_no:  Dec   edx;  Jnz   _inner;  Loop   _outter;  }  }

Bubble sort Result



Implementation of fast sorting algorithm with inline assembly

Memory position in stack   stack is extended by high address to low address//esp-xxx//Note parameter names do not conflict with inline assembly keywords void  quicksortasm (int arr[],int lowindex,int Highindex) {     int*p=arr; //For addressing convenience     int  begin;//    int  end;     __asm    {         //;mov eax,[ebp+16] Stack high Address low       //Use register        mov   eax,lowindex ; Index first       mov   ebx,highindex; index last       cmp   eax,ebx ; procedure         jg    _exit_func          mov   esi,p        ; storage array address-1 for addressing use        mov   edx,[esi+4*eax]           Storage key_outer:cmp   eax,ebx          ; If index first >index end        jnb   _endlable         ;  not eligible to exit                         _find1:cmp   eax,ebx            go to the first layer cycle        jnb   _inner1            conditions 1      ; Looking for the first value less than key from the back        cmp   [esi+4*ebx],edx ; compare array elements and edx       jb    _inner1 from behind           find the first array element less than key on the right side        Sub    ebx,1           , last index-1  last= last-1       jmp  _find1           ; jump to Loop head _inner1:       mov ecx,[ esi+4*ebx]   : Find less than Exchange value        mov [esi+4*eax],ecx_find2:cmp   eax, ebx          ; Go to the first layer of circulation         JNB    _inner2          ; conditions 1         ; After looking for the first value less than key         cmp   [esi+4*eax],edx ; Find the first element greater than key from the left         jg    _inner2           for signed numbers         add   eax,1             First index+1          jmp   _find2            jump to the loop head _inner2:   &Nbsp;   mov ecx,[esi+4*eax]    ; will first be greater than and axis exchanged        mov [esi+4* ebx],ecx       jmp _outer;_endlable:       mov [esi+4*eax],edx   Axis Reset        //recursive    parameters free to left        mov   Begin,eax;       mov  end,ebx       }//  Quicksortasm (arr,lowindex,begin-1);//    quicksortasm (arr,begin+1,highindex);    _ asm    {        mov    ecx,begin ; recursive 1        sub    ecx,1     ; index-1          push   ecx       ;        mov    Edx,lowindex;          push   edx          mov    eax,arr         push   eax          call         quicksortasm        add          esp,0ch   ; stack balancing         mov          ecx,highindex , recursive 2        push         ecx          mov          edx,begin          add          edx,1          push        edx           mov         Eax,arr   &nBsp      push        eax          call        quicksortasm         add          esp,0ch  ; Recovery stacks     }_exit_func:     return;}



the C + + implementation of the Bubbling sorting algorithm:

Time Complexity best   T (n) =o (n) ~o (n*n)   stability algorithm    void  Sortbubblec (int arr[],int sizen) {     bool Flag=true;//If no exchange exits directly      int tem;     for (int i=0;i<sizen-1&flag;i++)      {            flag=false;       & nbsp for (int j=0;j<sizen-i-1;j++)//Inner order bubbling          {        & nbsp       if (arr[j]>arr[j+1])              {                    flag=true;                  tem=arr[j];                  arr[j]=arr[j+1];                  arr[j+1]=tem;                           }         }      }}




Implementation of the fast sorting algorithm for C + +

Quick sort of C/C + + void  quicksortc (int arr[],int low,int high) {    if (low>high) return; int Begin=low, end=high,key= Arr[low];while (Begin<end) {while (Begin<end&&arr[end]>=key)--end;arr[begin]=arr[end];while ( Begin<end&&arr[begin]<=key) ++begin;arr[end]=arr[begin];} Arr[begin]=key; QUICKSORTC (arr,0,begin-1); QUICKSORTC (Arr,begin+1,high);}





QQ 4223665 Technology Exchange Group 387761601

Welcome everyone to Exchange learning software technology!


inline assembly vs. C + + implementation of bubble sorting, fast sorting algorithm sorting 500W data comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.