Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

ASM MyVal() - Optimized for special case

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin ASM MyVal() - Optimized for special case

    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION MyVal( sSource AS STRING) AS LONG
    
    	'Assumes sSource contains only characters from "0" to "9" and %MaxLong >= VAL( sSource) >= 0
    
    	#REGISTER NONE
    	LOCAL psSource AS LONG
    
    	psSource = STRPTR( sSource)
    
    	!mov   esi, psSource
    	!mov   ecx, [esi-4]      ; get size of string data
    
    	!xor   eax, eax          ; clear EAX
    	!xor   ebx, ebx          ; clear EBX
    	!mov   edi, 10           ; base 10
    
    	!test  ecx, ecx
    	!jz    Done
    
    NextChar:
    	!mov   bl, [esi]         ; get a character
    	!mul   edi               ; eax = eax * 10
    	!sub   bl, 48            ; convert character to binary digit
    	!add   eax, ebx          ; add in the digit
    	!inc   esi
    	!dec   ecx
    	!jnz   NextChar
    
    Done:
    	!mov   FUNCTION, eax
    
    END FUNCTION
    
    %NumTests = 1000000
    
    FUNCTION PBMAIN() AS LONG
    
    	LOCAL a$, I AS LONG, J AS LONG
    	LOCAL qValTix AS QUAD, qMyValTix AS QUAD
    
    	RANDOMIZE TIMER
    
    	'Does it work?
    	DO
    		a$ = FORMAT$( RND( 0, 2147483647&))
    		I = MyVal(a$)
    	LOOP UNTIL MSGBOX( "a$ = " + $DQ + a$ + $DQ + $CRLF + $CRLF + "MyVal( a$) = " + FORMAT$( I) + $CRLF + $CRLF + "Test again?", %MB_ICONQUESTION OR %MB_YESNO, "MyVal") = %IDNO
    
    	'Benchmarking...
    	DO
    		a$ = FORMAT$( RND( 0, 2147483647&))
    
    		TIX qValTix
    		FOR J = 1 TO %NumTests
    			I = VAL(a$)
    			I = VAL(a$)
    			I = VAL(a$)
    		NEXT
    		TIX END qValTix
    
    		TIX qMyValTix
    		FOR J = 1 TO %NumTests
    			I = MyVal(a$)
    			I = MyVal(a$)
    			I = MyVal(a$)
    		NEXT
    		TIX END qMyValTix
    
    	LOOP UNTIL MSGBOX( "VAL() took " + FORMAT$( qValTix, "#,") + " tix." + $CRLF + $CRLF + "MyVal() took " + FORMAT$( qMyValTix, "#,") + " tix." + $CRLF + $CRLF + "Test again?", %MB_ICONQUESTION OR %MB_YESNO, "MyVal") = %IDNO
    
    END FUNCTION
    Bernard Ertl
    InterPlan Systems
Working...
X