faster implementation of sum ( for Codility test )

前端 未结 22 2149
鱼传尺愫
鱼传尺愫 2021-02-04 11:47

How can the following simple implementation of sum be faster?

private long sum( int [] a, int begin, int end ) {
    if( a == null   ) {
        ret         


        
22条回答
  •  庸人自扰
    2021-02-04 12:23

    {In Pascal + Assembly}
    {$ASMMODE INTEL}
    function equi (A : Array of longint; n : longint ) : longint;
    var c:Longint;
        label noOverflow1;
        label noOverflow2;
        label ciclo;
        label fine;
        label Over;
        label tot;
    Begin
     Asm
        DEC n
        JS over
        XOR ECX, ECX   {Somma1}
        XOR EDI, EDI   {Somma2}
        XOR EAX, EAX
        MOV c, EDI
        MOV ESI, n
      tot:
        MOV EDX, A
        MOV EDX, [EDX+ESI*4]
        PUSH EDX
        ADD ECX, EDX
        JNO nooverflow1
        ADD c, ECX
        nooverflow1:
        DEC ESI
      JNS tot;
        SUB ECX, c
        SUB EDI, c
      ciclo:
        POP EDX
        SUB ECX, EDX
        CMP ECX, EDI
        JE fine
        ADD EDI, EDX
        JNO nooverflow2
        DEC EDI
        nooverflow2:
        CMP EAX, n
        JA over
        INC EAX
        JMP ciclo
        over:
          MOV EAX, -1
        fine:
      end;
    End;
    

提交回复
热议问题