» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

A simple array addition

Even with simple things as adding one number to every tiem to the array, VB proves no competition against inline ASM
the simple ASM code easily beats VB with 400%. Also note that if I'd cache the pointer to the array, it would be even higher.
Since then, I only have to call VarPtr only once, instead of every iteration. But I thought this would be more fair "against" VB.
I used an array which contained 20.000 items.


'//How-to use: Simply call it like:
'//Call mdlASM.UpdateArrayASM(VarPtr(myArray(0)), UBound(myArray), lngNumber)
'//lngNumber is the number to add to the array.

Public Function UpdateArrayASM(ByVal lngPointer As Long, ByVal lngUbound As Long, ByVal lngNumber As Long) As Long
'#ASM_START
' push ebp
' mov ebp, esp
' push ecx
' push edx
' push eax

' mov eax, dword ptr[ebp+8] ;Array pointer
' mov ecx, [ebp+12] ;Elements
' mov edx, [ebp+16] ;Number to add

' ;Loop label
'LoopArray:
' add dword ptr[eax], edx ;Add lngNumber to the pointer
' add eax, 4 ;Increase our position in the array (element, steps of 4 because of long)
'
' dec ecx ;Decrease ecx with one (like ecx--)
' jnz LoopArray ;"goto" LoopArray label.. as long as ecx is not zero

' pop eax
' pop edx
' pop ecx
' mov esp, ebp ;MOV/POP is much faster
' pop ebp ;on 486 and Pentium than Leave
' ret 8
'#ASM_END
End Function


Public Function UpdateArrayVB(myArray() As Long, ByVal lngNumber As Long) As Long
Dim Y As Long

For Y = 0 To UBound(myArray)
myArray(Y) = myArray(Y) + lngNumber
Next Y

End Function

VBArrayAdd % faster than ASMArrayAdd VBArrayAdd (sec) ASMArrayAdd (sec)
449.8% 4.882183 0.888067
350.2% 4.809162 1.068218
446.4% 4.831393 0.88426
413.1% 4.890543 0.9531
441.1% 4.854989 0.89723

VBArrayAdd % faster than ASMArrayAdd VBArrayAdd (sec) ASMArrayAdd (sec)
436.4% 9.784413 1.824087
456.1% 9.761054 1.755358
450.4% 9.902339 1.799235
468.8% 10.117369 1.778716
447.8% 9.757736 1.781121


User contributed notes:

Author: Tom (hurendo_kun at hotmail dot com) Date: 19:07 14/07/2005
You're only calling VarPtr once, in the initial function call. The value is cached automatically when it's passed ByVal. So, this test is really as fast as it can get.

Author: Percephone () Date: 15:01 18/01/2009
I think you should distribute the source code or make its location known, Before VB6 becomes extinct. Heck, we might even be able to further the optimization of the library in the public domain.

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: