Guidance
指路人
g.yi.org
software / rapidq / Examples / Algorithm & Maths / bitwise shift.bas

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
'From:  Jacques Philippe <jp@t...>  Sun Sep 1, 2002  6:01 pm

' cR * 65536 is the fastest, then cR SHL 16 then cr * 256 * 256
' I prove it :), try this
' an ASM loop, is 3000 time faster.. but a 3000 time useless demo too :)

' ---- CODE START ----
     $ESCAPECHARS ON
     $TYPECHECK ON
     $INCLUDE "RAPIDQ.INC"
'
     DECLARE FUNCTION CallAsmProc LIB "user32" ALIAS "CallWindowProcA" _
      (Proc AS LONG, A1 AS LONG, A2 AS LONG, A3 AS LONG, _
      A4 AS LONG) AS LONG
'
' ============================================================
' ----- START ASM TestShift16 -----
'
' RqAsm code
' ----------
' function TestShift16 number iloop
' mov ecx, iloop
' .iStart:
' mov eax, number
' shl eax, 16
' loop .iStart, ecx
' end function
'
     DEFBYTE TestShift16Array (0 TO 18) = _
      { _
      &HC8, &H00, &H00, &H00, &H8B, &H4D, &H0C, &H8B, &H45, &H08, _
      &HC1, &HE0, &H10, &HE2, &HF8, &HC9, &HC2, &H10, &H00 _
      }
' ----- END ASM TestShift16 -----
'
' ----- POINTER to use In CallAsmProc -----
' A Bit Faster than Calling TestShift16
     DEFINT ptrTestShift16
     ptrTestShift16 = VARPTR (TestShift16Array(0))
'
' ----- RQ CALL TestShift16 -----
     FUNCTION TestShift16 (Arg1 AS LONG, Arg2 AS LONG) AS LONG
      Result = CallAsmProc (ptrTestShift16, Arg1, Arg2, 0, 0)
     END FUNCTION
'
' ============================================================
'
     DEFSNG sStart, sEnd, SHLTime, MUL256Time, MUL65536Time, ASMTime
     DEFINT N, iCount, i
'
     iCount = 500000
     i = 123
'
     PRINT " Can take some time\n 500000 Loops\n - i SHL 16\n - i * 256 * 256\n - i * 65536\n - i SHL 16 in ASM\n\n ... be Patient\n"
     sStart = TIMER
     FOR N = 1 TO iCount
      i=i SHL 16
     NEXT N
     sEnd = TIMER
     PRINT (STR$(iCount) & " Loops i SHL 16 Time : " & STR$(sEnd - sStart))
     SHLTime = sEnd - sStart
'
     sStart = TIMER
     FOR N = 1 TO iCount
      i = i * 256 * 256
     NEXT N
     sEnd = TIMER
     PRINT (STR$(iCount) & " Loops i * 256 * 256 Time : " & STR$(sEnd - sStart))
     MUL256Time = sEnd - sStart
'
     sStart = TIMER
     FOR N = 1 TO iCount
      i = i * 65536
     NEXT N
     sEnd = TIMER
     PRINT (STR$(iCount) & " Loops i * 65536 Time : " & STR$(sEnd - sStart))
     MUL65536Time = sEnd - sStart
'
     sStart = TIMER
     i = TestShift16 (i, iCount)
     sEnd = TIMER
     PRINT (STR$(iCount) & " Loops Shift Asm Time : " & STR$(sEnd - sStart))
     ASMTime = sEnd - sStart
     PRINT ("\nASM is " & STR$(INT(MUL65536Time/ASMTime)) & " Time Faster than *65536")
'
' -------------------------------------------------------------
' EXIT CONSOLE
' ------------
     DEFSTR sExit
     INPUT "\n\n CR to QUIT \n\n", sExit
     Application.Terminate
     END
' ---- END of CODE -----
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sun 2022-9-25  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2009-07-25 17:30:54