ByVal vs. ByRef -- preformance 
Author Message
 ByVal vs. ByRef -- preformance

I have a component that has several routines passing a scripting.dictionary
object back and forth. Some will make modifications to the dictionary
object.

My question is...is there any performance difference in ByVal and ByRef...
Its all in one component. I've read that I should use ByVal if doing remote
objects.. Currently everything is ByVal and I'm wondering if I can squeeze
any more speed out of it.

any ideas??

thanks,
Stephen



Mon, 03 May 2004 05:26:40 GMT  
 ByVal vs. ByRef -- preformance
ByVal and ByRef are REALLY DIFFERENT.... and I beleive it is REALLY
IMPORTANT to understand how they work to use them properly

ByVal places in the stack the VALUE of the passed variable. If you pass an
array, it will push the all array on the stack. This will take time (push
AND pop) and may exaust the stack space

byReg will place only the pointer on the stack, BUT any change  in the data
will reflect, of course, on the caller as well....

Read the standard docs from VB about it and understand it, it may help you a
lot to optimize your apps...

stef


Quote:
> I have a component that has several routines passing a

scripting.dictionary
Quote:
> object back and forth. Some will make modifications to the dictionary
> object.

> My question is...is there any performance difference in ByVal and ByRef...
> Its all in one component. I've read that I should use ByVal if doing
remote
> objects.. Currently everything is ByVal and I'm wondering if I can squeeze
> any more speed out of it.

> any ideas??

> thanks,
> Stephen



Mon, 03 May 2004 06:01:53 GMT  
 ByVal vs. ByRef -- preformance

Quote:
> ByVal and ByRef are REALLY DIFFERENT.... and I beleive it is REALLY
> IMPORTANT to understand how they work to use them properly

This is very good advice, however..

Quote:


> > I have a component that has several routines passing a
> scripting.dictionary

In this case, the *value* that you are passing around is the "address" of
the dictionary
(it is an object rather than an "value" type (ie string, or long))

If you pass objects ByVal, changing the objects properties, etc *will* still
affect
the object as passed in, but if you change the object that the variable
references
*this* will not be reflected back to the caller

ie

    Private Sub SubA( ByVal objX As Dictionary)
        objX.RemoveAll
    End Sub
and
    Private Sub SubA( ByRef objX As Dictionary)
        objX.RemoveAll
    End Sub

will behave in a similar fashion, despite the ByRef/ByVal difference

However,
    Private Sub SubA( ByVal objX As Dictionary)
        Set objX = Nothing
    End Sub
and
    Private Sub SubA( ByRef objX As Dictionary)
        Set objX = Nothing
    End Sub

will leave the caller with Nothing in the ByRef instance, but leave it
untouched
in the ByVal case.  The difference is that the *pointer* to the object has
changed
rather than the content of the object

I hope that makes it clear (perhaps read the above after digesting the docs
in the differences)

BTW in VB pre .NET, the default is ByRef, but the default in .NET is ByVal
so it makes sense to be explicit in all cases and not rely on the defaults
(It also makes it clear, that it's been considered)

--
Chris Anderson
(aka Merak on VISBAS-L)



Mon, 03 May 2004 06:19:32 GMT  
 ByVal vs. ByRef -- preformance

Quote:
> I have a component that has several routines passing a

scripting.dictionary

Quote:
> object back and forth. Some will make modifications to the dictionary
> object.

> My question is...is there any performance difference in ByVal and ByRef...

Stephen,

As others have already pointed out, the routines will be able to modify the
Dictionary regardless of whether it's ByRef or ByVal.

As far as performance, ByRef is faster.  IMHO it's safe to use when it's all
your code, and you know you won't be "accidentally" modifying a value.  It's
faster because only a 32-bit pointer is passed, rather than, say, a 64-bit
Double, a long string, or (God forbid) a big fat Variant.  I've heard it's
even faster for 16-bit Integers and Booleans, just because the processor is
32 bits and things work that way.

Now, object references are 32 bits.  When you pass it ByRef, a pointer to
the reference is handed over, and with ByVal, a copy of the reference is
passed.  Either way it's 32 bits, but ByRef is still faster!  The reason has
to do with COM semantics.  What is a "copy of a reference"?  VB actually has
to call QueryInterface on the object when it's passed in ByVal, and this
takes time.

And as you've already heard, using ByRef across processes or machines is
+very+ bad, especially with objects.  The marshalling just kills
performance.

-- Matthew Solnit



Mon, 03 May 2004 21:54:28 GMT  
 ByVal vs. ByRef -- preformance
Stef,

Quote:
>ByVal and ByRef are REALLY DIFFERENT.... and I beleive it is REALLY
>IMPORTANT to understand how they work to use them properly

Interesting that you should say that...

Quote:
>ByVal places in the stack the VALUE of the passed variable. If you pass an
>array, it will push the all array on the stack. This will take time (push
>AND pop) and may exaust the stack space

... and then be so wrong here. ;-) You can't even pass an array ByVal
in VB. The only way you can get an array into a ByVal parameter is if
it's declared as Variant, in which case it will always take up 16
bytes of stack space, no matter how big the array is.

Mattias

===
Mattias Sj?gren (VB MVP)

http://www.msjogren.net/dotnet/



Mon, 03 May 2004 23:12:41 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. byVal vs ByRef

2. ByVal vs ByRef

3. Passing Parameters - A ByRef vs ByVal Question

4. Byval vs Byref

5. byval vs. byref

6. ByRef Vs ByVal

7. Override ByRef with ByVal

8. ByVal / ByRef

9. Questions regarding objects, assignments, byval, byref, etc...

10. Change Standard from ByVal to ByRef

11. Event Arguments : ByRef or ByVal (Which Best)?

12. Peformance in VB.NET - ByRef and ByVal

 

 
Powered by phpBB® Forum Software