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

Variant returning functions

Another proof how slow variants really are.

I had one function return a single, and the other function returning a variant (no "As" behind it).

Code:
Public Function TestOne(ByRef A As Single) As Single '//Return as single TestTwo = A + A End Function Public Function TestTwo(ByRef A As Single) '//Return as variant TestOne = A + A End Function

Variant % faster than Single Variant (sec) Single (sec)
190.5% 0.113906 0.03921
371.9% 0.114672 0.0243
337.1% 0.113121 0.02588
411.1% 0.135781 0.026568
408.7% 0.129636 0.025484


User contributed notes:

Author: VBBR () Date: 22:03 31/03/2004
Hey there's something messed up in the code... It seems TestOne returns to TestTwo and vice versa... And the table says that Variant is FASTER than single.

Author: Almar Joling () Date: 22:03 31/03/2004
Yeah, I might have changed it accidentally somehow...

But the single is faster, as the table and graph show.

Author: BYTE-Smasher (bytesmasher at gmail dot com) Date: 21:11 16/11/2004
Uuhm... how many iterations did you use? Because my tests showed a very small difference between the two... at 10000000 iterations, single returned 5.066406 seconds... variant returned 5.140625 seconds... a ratio of 1.014 : 1... not much of a difference at all... :s

Author: Almar Joling () Date: 21:11 16/11/2004
Probably 500.000. Are you sure you did it the right way? :).

Author: BYTE-Smasher () Date: 21:11 16/11/2004
very sure... in fact, when I compiled to exe, the results became almost identical...

here's my code:
Private Sub Form_Load()
Dim A As Long
Dim B As Long
t = Timer
For A = 1 To 10000000
B = TestOne(10)
Next
MsgBox (Timer - t)
t = Timer
For A = 1 To 10000000
B = TestOne(10)
Next
MsgBox (Timer - t)
End Sub
Public Function TestOne(ByRef A As Single) As Single
'//Return as single
TestOne = A + A
End Function

Public Function TestTwo(ByRef A As Single)
'//Return as variant
TestTwo = A + A
End Function

Author: Almar Joling () Date: 21:11 16/11/2004
The timer function is way to inaccurate. Check out QueryPerformancecounter. You can download my template here:

http://www.persistentrealities.com/getfile.php?file=vbftemplate.zip

Author: BYTE-Smasher () Date: 22:11 16/11/2004
I completely realise that the timer function is "innacurate" at best, but I just put the above code together quicky to demonstrate a point... I've since retried with QueryPerformanceCounter and gotten similar results...

My new code is here:
http://www.nomorepasting.com/paste.php?pasteID=24991

Author: Tom (hurendo_kun at hotmail dot com) Date: 14:05 31/05/2005
My test results are also almost identical. Single is faster by a ratio of approximately 1.0000002 to 1. Almar, I'd like to see your original project so we can clear this matter up.

Author: Almar () Date: 20:05 31/05/2005
Hm.

I have to agree this time, something is odd here. I think that something got mixed up when I went to version two of this site. The above code is a comparison between VB code and an ASM piece of code.

Author: Tom (hurendo_kun at hotmail dot com) Date: 16:06 08/06/2005
I'd have to look at the descriptor for a Variant, but it occurs to me that in this case, the function probably returns a pointer to the variant, which is 32 bits (same as a single). Whatever slight difference we've noticed is probably incurred while the pointer is extracted and assigned to EAX. Anyway, I suggest avoiding Variants whenever possible, and I think Almar agrees.

Author: Tanner Helland (tannerhelland at hotmail dot com) Date: 23:08 23/08/2006
To my knowledge, VB will always return the value from a function ByRef. When a call to a function is placed, a spot is reserved for the returned variable on the runtime stack. Thus, when the function is over, a pointer to the already allocated spot on the stack is returned.

And as most programmers here should know, a pointer is 32-bits regardless of what it points to. So yes, there should be no speed difference when it comes to returning the value (except for the minute requirement for setting more bytes in memory for a Variant....but that probably isn't even detectable).

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