» 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!

Average array, ASM vs VB

This code will simply average the numbers in an array, and return the integer result (no floating points yet ;-)
It's quite a bit faster than the VB variant, which does exactly the same :)

VB version:


Public Function AvgArrayVB(myArray() As Long) As Integer
Dim Y As Long
Dim lngTotal As Long

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

AvgArrayVB = lngTotal / (UBound(myArray) + 1)
End Function


ASM Version:

Public Sub asmAverageArray(ByVal lngPointer As Long, ByVal lngUbound As Long, ByRef intAverage As Integer)
'#ASM_START
' push ebp
' mov ebp, esp

' mov eax, dword ptr[ebp+8] ;Array pointer
' mov ecx, [ebp+12] ;Elements
' mov edx, 0
' ;Loop label
'LoopArray:
' add edx, dword ptr[eax] ;Add the array number to the edx register
' 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

' add edx, dword ptr[eax] ;Last number, which falls out of the loop..
' mov eax, edx ;Mov edx to eax
' mov ecx, [ebp+12]
' mov edx, 0


' div ecx ;Divide by elements
' inc eax ;We need to add 1 somehow
' mov edx, [ebp+16] ;move pointer to ecx
' mov [edx], eax ;Move result to ecx pointer
'
' mov esp, ebp ;MOV/POP is much faster
' pop ebp ;on 486 and Pentium than Leave
' ret 8
'#ASM_END
End Sub

VBArrayAdd % faster than ASMArrayAdd VBArrayAdd (sec) ASMArrayAdd (sec)
146.8% 0.376513 0.152577
125.8% 0.36739 0.162725
152.7% 0.387316 0.153288
145.3% 0.394099 0.160685
161.7% 0.418255 0.159807

VBArrayAdd % faster than ASMArrayAdd VBArrayAdd (sec) ASMArrayAdd (sec)
122.1% 2.244805 1.010599
137.2% 2.275644 0.959576
114% 2.115267 0.988646
124.3% 2.144538 0.956041
118% 2.250119 1.032332

VBArrayAdd % faster than ASMArrayAdd VBArrayAdd (sec) ASMArrayAdd (sec)
133.5% 4.069179 1.742685
117.5% 3.958643 1.820075
123.4% 3.849878 1.723398
125.5% 3.821168 1.694861
129.3% 3.753615 1.637236


User contributed notes:

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