ByVal or Byref : performance question ? 
Author Message
 ByVal or Byref : performance question ?

What is the fastest way to pass a control reference to a method ?
ByVal or Byref.

Knowing the following informations from the book "{*filter*} Visual Basic"
from Microsoft Press :

Problem Native Code P-Code
Integer by value 0.0063 sec 0.2508 sec
Integer by reference 0.0069 sec 0.2518 sec
Long by value 0.0073 sec 0.2563 sec
Long by reference 0.0072 sec 0.2553 sec
Single by value 0.0072 sec 0.2521 sec
Single by reference 0.0073 sec 0.2585 sec
Double by value 0.0078 sec 0.2579 sec
Double by reference 0.0073 sec 0.2599 sec
Variant by value 0.1585 sec 0.5561 sec
Variant by reference 0.0602 sec 0.4222 sec
String by value 0.3388 sec 0.8279 sec
String by reference  0.1669 sec 0.5901 sec

Control by value ?
Control by reference ?
Object by value ?
Object by reference ?
...

Please, if you have any ideas complete !



Mon, 21 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
Jean-Philippe

May I suggest that you don't worry about timings but concentrate on the
functionality. In general terms, use ByVal where you wish to protect the
argument from being written to and ByRef where it needs to be updated. There
are certain arguments (controls, arrays, etc.) which must be passed ByRef
but try to use the 'correct' one, not the fastest.

--
Regards
{*filter*}

Remove '!no!spam!' to reply by e-mail but reply to newsgroup as well.


Quote:
> What is the fastest way to pass a control reference to a method ?
> ByVal or Byref.

> Knowing the following informations from the book "{*filter*} Visual Basic"
> from Microsoft Press :

> Problem Native Code P-Code
> Integer by value 0.0063 sec 0.2508 sec
> Integer by reference 0.0069 sec 0.2518 sec
> Long by value 0.0073 sec 0.2563 sec
> Long by reference 0.0072 sec 0.2553 sec
> Single by value 0.0072 sec 0.2521 sec
> Single by reference 0.0073 sec 0.2585 sec
> Double by value 0.0078 sec 0.2579 sec
> Double by reference 0.0073 sec 0.2599 sec
> Variant by value 0.1585 sec 0.5561 sec
> Variant by reference 0.0602 sec 0.4222 sec
> String by value 0.3388 sec 0.8279 sec
> String by reference  0.1669 sec 0.5901 sec

> Control by value ?
> Control by reference ?
> Object by value ?
> Object by reference ?
> ...

> Please, if you have any ideas complete !



Mon, 21 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
I do not totally agree with you, to manipule efficient strings in VB you
need to pass strings by reference even you do not need to modify them in a
method !

Read this from the book "{*filter*} Visual Basic" from Microsoft Press :

Conclusion: For intrinsic numeric types, there isnt enough difference to
spit at. Notice what happens as the variables get larger. A reference
variable is always- four bytes, but if you have to push all eight bytes of a
Double or all 16 bytes of a Variant onto the stack, its going to cost you.
I threw strings into the table for comparison, but in fact they work a
little differently. Youre not really saving the whole string on the stack
when you pass a string by value. You are making an extra copy thoughthrough
a mechanism that we wont worry about in this chapter. Just take a look at
the extra cost, and then make the obvious decision: always pass strings by
reference in your Basic code.

Regards.

JP.

Quote:

>Jean-Philippe

>May I suggest that you don't worry about timings but concentrate on the
>functionality. In general terms, use ByVal where you wish to protect the
>argument from being written to and ByRef where it needs to be updated.
There
>are certain arguments (controls, arrays, etc.) which must be passed ByRef
>but try to use the 'correct' one, not the fastest.

>--
>Regards
>Dick

>Remove '!no!spam!' to reply by e-mail but reply to newsgroup as well.



>> What is the fastest way to pass a control reference to a method ?
>> ByVal or Byref.

>> Knowing the following informations from the book "{*filter*} Visual Basic"
>> from Microsoft Press :

>> Problem Native Code P-Code
>> Integer by value 0.0063 sec 0.2508 sec
>> Integer by reference 0.0069 sec 0.2518 sec
>> Long by value 0.0073 sec 0.2563 sec
>> Long by reference 0.0072 sec 0.2553 sec
>> Single by value 0.0072 sec 0.2521 sec
>> Single by reference 0.0073 sec 0.2585 sec
>> Double by value 0.0078 sec 0.2579 sec
>> Double by reference 0.0073 sec 0.2599 sec
>> Variant by value 0.1585 sec 0.5561 sec
>> Variant by reference 0.0602 sec 0.4222 sec
>> String by value 0.3388 sec 0.8279 sec
>> String by reference  0.1669 sec 0.5901 sec

>> Control by value ?
>> Control by reference ?
>> Object by value ?
>> Object by reference ?
>> ...

>> Please, if you have any ideas complete !



Mon, 21 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
Controls, Objects and other references are always pointers so passing them
ByVal or ByRef doesn't make much difference. ByVal passes the reference
itself (a pointer), ByRef passes a pointer to the reference (a pointer to a
pointer). When calling an out-of-process server using ByVal is faster
because the pointer doesn't have to be passed back to your program (the same
is true for strings and other values).

Regards,
Roel de Regt


Quote:
> What is the fastest way to pass a control reference to a method ?
> ByVal or Byref.

> Knowing the following informations from the book "{*filter*} Visual Basic"
> from Microsoft Press :

> Problem Native Code P-Code
> Integer by value 0.0063 sec 0.2508 sec
> Integer by reference 0.0069 sec 0.2518 sec
> Long by value 0.0073 sec 0.2563 sec
> Long by reference 0.0072 sec 0.2553 sec
> Single by value 0.0072 sec 0.2521 sec
> Single by reference 0.0073 sec 0.2585 sec
> Double by value 0.0078 sec 0.2579 sec
> Double by reference 0.0073 sec 0.2599 sec
> Variant by value 0.1585 sec 0.5561 sec
> Variant by reference 0.0602 sec 0.4222 sec
> String by value 0.3388 sec 0.8279 sec
> String by reference  0.1669 sec 0.5901 sec

> Control by value ?
> Control by reference ?
> Object by value ?
> Object by reference ?
> ...

> Please, if you have any ideas complete !



Wed, 23 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
Is this one of those 'How many mistakes can you find in the picture?'
posts? <g>  Okay, I see one phrase that is true . . .  how many do you
see?
Quote:

> Controls, Objects and other references are always pointers so passing them
> ByVal or ByRef doesn't make much difference. ByVal passes the reference
> itself (a pointer), ByRef passes a pointer to the reference (a pointer to a
> pointer). When calling an out-of-process server using ByVal is faster
> because the pointer doesn't have to be passed back to your program (the same
> is true for strings and other values).

> Regards,
> Roel de Regt



> > What is the fastest way to pass a control reference to a method ?
> > ByVal or Byref.

> > Knowing the following informations from the book "{*filter*} Visual Basic"
> > from Microsoft Press :

> > Problem Native Code P-Code
> > Integer by value 0.0063 sec 0.2508 sec
> > Integer by reference 0.0069 sec 0.2518 sec
> > Long by value 0.0073 sec 0.2563 sec
> > Long by reference 0.0072 sec 0.2553 sec
> > Single by value 0.0072 sec 0.2521 sec
> > Single by reference 0.0073 sec 0.2585 sec
> > Double by value 0.0078 sec 0.2579 sec
> > Double by reference 0.0073 sec 0.2599 sec
> > Variant by value 0.1585 sec 0.5561 sec
> > Variant by reference 0.0602 sec 0.4222 sec
> > String by value 0.3388 sec 0.8279 sec
> > String by reference  0.1669 sec 0.5901 sec

> > Control by value ?
> > Control by reference ?
> > Object by value ?
> > Object by reference ?
> > ...

> > Please, if you have any ideas complete !



Wed, 23 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
I see two:

Quote:
> Controls, Objects and other references are always pointers

A variable whose value is interpreted as a memory address is a pointer.
Although we use a control's (object's) name to access its properties,
the named variable (AFAIK) actually contains a base address.

Quote:
> When calling an out-of-process server using ByVal is faster

Obviously, less re-direction will result in faster performance.

To help answer the question, being passed as a variable, I would think
declaring the parameter type would be the best insurance for increased
performance, as VB can then use the base address, and that objects
Type Library to quickly access its properties and methods.

LFS

Quote:

> Is this one of those 'How many mistakes can you find in the picture?'
> posts? <g>  Okay, I see one phrase that is true . . .  how many do you
> see?


> > Controls, Objects and other references are always pointers so passing them
> > ByVal or ByRef doesn't make much difference. ByVal passes the reference
> > itself (a pointer), ByRef passes a pointer to the reference (a pointer to a
> > pointer). When calling an out-of-process server using ByVal is faster
> > because the pointer doesn't have to be passed back to your program (the same
> > is true for strings and other values).

> > Regards,
> > Roel de Regt




Wed, 23 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
Here is the result of our reflexions :

http://www.experts-exchange.com/Computers/Programming/Windows/Visual_...
.10191014

Regards.

Jean-Philippe FORTI.



Fri, 25 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
Sure, post the question, get a few answers, then delete it!

Although the exchange is an interesting concept, I find the
newsgroups are easier to navigate...

LFS

Quote:

> Here is the result of our reflexions :

> http://www.experts-exchange.com/Computers/Programming/Windows/Visual_...
> .10191014

> Regards.

> Jean-Philippe FORTI.



Fri, 25 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
Jean-Philippe

Quote:
> ...to manipule efficient strings in VB you
> need to pass strings by reference even you do not need to modify them in a
> method !

That's nonsense. You must protect your data above all. If I pass a string to
a function of any kind, I may want to ensure that it is not overwritten. If
I'm writing the function, I would make the argument ByVal to ensure this. If
I'm writing the function's client, I would enclose the argument in
parentheses (to pass it ByVal).

Performance would never be considered.

--
Regards
{*filter*}

Remove '!no!spam!' to reply by e-mail but reply to newsgroup as well.


Quote:
> I do not totally agree with you, to manipule efficient strings in VB you
> need to pass strings by reference even you do not need to modify them in a
> method !

> Read this from the book "{*filter*} Visual Basic" from Microsoft Press :

> Conclusion: For intrinsic numeric types, there isn't enough difference to
> spit at. Notice what happens as the variables get larger. A reference
> variable is always- four bytes, but if you have to push all eight bytes of
a
> Double or all 16 bytes of a Variant onto the stack, it's going to cost
you.
> I threw strings into the table for comparison, but in fact they work a
> little differently. You're not really saving the whole string on the stack
> when you pass a string by value. You are making an extra copy
though-through
> a mechanism that we won't worry about in this chapter. Just take a look at
> the extra cost, and then make the obvious decision: always pass strings by
> reference in your Basic code.

> Regards.

> JP.


> >Jean-Philippe

> >May I suggest that you don't worry about timings but concentrate on the
> >functionality. In general terms, use ByVal where you wish to protect the
> >argument from being written to and ByRef where it needs to be updated.
> There
> >are certain arguments (controls, arrays, etc.) which must be passed ByRef
> >but try to use the 'correct' one, not the fastest.

> >--
> >Regards
> >Dick

> >Remove '!no!spam!' to reply by e-mail but reply to newsgroup as well.



> >> What is the fastest way to pass a control reference to a method ?
> >> ByVal or Byref.

> >> Knowing the following informations from the book "{*filter*} Visual
Basic"
> >> from Microsoft Press :

> >> Problem Native Code P-Code
> >> Integer by value 0.0063 sec 0.2508 sec
> >> Integer by reference 0.0069 sec 0.2518 sec
> >> Long by value 0.0073 sec 0.2563 sec
> >> Long by reference 0.0072 sec 0.2553 sec
> >> Single by value 0.0072 sec 0.2521 sec
> >> Single by reference 0.0073 sec 0.2585 sec
> >> Double by value 0.0078 sec 0.2579 sec
> >> Double by reference 0.0073 sec 0.2599 sec
> >> Variant by value 0.1585 sec 0.5561 sec
> >> Variant by reference 0.0602 sec 0.4222 sec
> >> String by value 0.3388 sec 0.8279 sec
> >> String by reference  0.1669 sec 0.5901 sec

> >> Control by value ?
> >> Control by reference ?
> >> Object by value ?
> >> Object by reference ?
> >> ...

> >> Please, if you have any ideas complete !



Fri, 25 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
Dick,
You're right, but to get access to good performances, we need to use byref
with strings and pay attention to not modify the strings during the calls !

JP.

Regards.

Quote:

>Jean-Philippe

>> ...to manipule efficient strings in VB you
>> need to pass strings by reference even you do not need to modify them in
a
>> method !

>That's nonsense. You must protect your data above all. If I pass a string
to
>a function of any kind, I may want to ensure that it is not overwritten. If
>I'm writing the function, I would make the argument ByVal to ensure this.
If
>I'm writing the function's client, I would enclose the argument in
>parentheses (to pass it ByVal).

>Performance would never be considered.

>--
>Regards
>Dick

>Remove '!no!spam!' to reply by e-mail but reply to newsgroup as well.



>> I do not totally agree with you, to manipule efficient strings in VB you
>> need to pass strings by reference even you do not need to modify them in
a
>> method !

>> Read this from the book "{*filter*} Visual Basic" from Microsoft Press :

>> Conclusion: For intrinsic numeric types, there isn't enough difference to
>> spit at. Notice what happens as the variables get larger. A reference
>> variable is always- four bytes, but if you have to push all eight bytes
of
>a
>> Double or all 16 bytes of a Variant onto the stack, it's going to cost
>you.
>> I threw strings into the table for comparison, but in fact they work a
>> little differently. You're not really saving the whole string on the
stack
>> when you pass a string by value. You are making an extra copy
>though-through
>> a mechanism that we won't worry about in this chapter. Just take a look
at
>> the extra cost, and then make the obvious decision: always pass strings
by
>> reference in your Basic code.

>> Regards.

>> JP.


>> >Jean-Philippe

>> >May I suggest that you don't worry about timings but concentrate on the
>> >functionality. In general terms, use ByVal where you wish to protect the
>> >argument from being written to and ByRef where it needs to be updated.
>> There
>> >are certain arguments (controls, arrays, etc.) which must be passed
ByRef
>> >but try to use the 'correct' one, not the fastest.

>> >--
>> >Regards
>> >Dick

>> >Remove '!no!spam!' to reply by e-mail but reply to newsgroup as well.



>> >> What is the fastest way to pass a control reference to a method ?
>> >> ByVal or Byref.

>> >> Knowing the following informations from the book "{*filter*} Visual
>Basic"
>> >> from Microsoft Press :

>> >> Problem Native Code P-Code
>> >> Integer by value 0.0063 sec 0.2508 sec
>> >> Integer by reference 0.0069 sec 0.2518 sec
>> >> Long by value 0.0073 sec 0.2563 sec
>> >> Long by reference 0.0072 sec 0.2553 sec
>> >> Single by value 0.0072 sec 0.2521 sec
>> >> Single by reference 0.0073 sec 0.2585 sec
>> >> Double by value 0.0078 sec 0.2579 sec
>> >> Double by reference 0.0073 sec 0.2599 sec
>> >> Variant by value 0.1585 sec 0.5561 sec
>> >> Variant by reference 0.0602 sec 0.4222 sec
>> >> String by value 0.3388 sec 0.8279 sec
>> >> String by reference  0.1669 sec 0.5901 sec

>> >> Control by value ?
>> >> Control by reference ?
>> >> Object by value ?
>> >> Object by reference ?
>> >> ...

>> >> Please, if you have any ideas complete !



Fri, 25 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
You have {*filter*} Visual Basic, find Chapter 10 - COM RAQs and
read the second section (pp. 572):

"What's the difference between ByVal and ByRef object parameters?"

In summary:
If you are working in-process "you might as well pass by reference".
If you are working out-of-process the major hit comes from marshalling
the data.  If you pass it ByVal, the data needs only to go one way.
If you pass it ByRef, it has to go both ways.  If you do not intend to
change the object, pass it ByVal.

Take note of the NOTE on page 574, type declarations of your parameters
will significantly impact performance.  I thought I said that... <G>

Sorry Jean, IMO the VBGuru deserved the points because he was right on,
as it pertains to object parameters....

If you are going out of process, and plan to change a few parameters,
then pass only those parameters, again marshalling the data is going
to be the most time consuming part of the calling process....

CC:E-mail and newsgroup
HTH
LFS

Quote:

> Visual Basic Question
> Title: "ByRef vs ByVal : performances"

> From: jpforti Date: Thursday, August 05 1999 - 02:21PM CEST
> Status: Waiting for answer
> Points: 150 Points

> What is the fastest way to pass a control reference to a method ?
> ByVal or Byref.

> Knowing the following informations from the book "{*filter*} Visual Basic"
> from Microsoft Press :

> ...

> Please, if you have any ideas complete !

> Question History:

> Comment
> From: VBGuru Date: Thursday, August 05 1999 - 03:04PM CEST

> Choosing Between ByVal and ByRef
> Because of marshaling considerations, certain method arguments should be
> declared ByVal for out-of-process components and ByRef for in-process
> components, as described below.
> Object References
> When youre declaring methods for objects provided by an out-of-process
> component, always use ByVal to declare arguments that will contain object
> references.
> The reason for this is that cross-process marshaling of object references
> requires significantly less overhead if its one-way. Declaring an argument
> ByRef means that the object reference must be marshaled to your component,
> and then back to the client when the method is finished.
> The only reason to use ByRef in the declaration of an argument thats going
> to contain an object reference is when the method will replace the clients
> object reference with a reference to another object.
> Dont Pass Object References When Properties Will Do
> If the method youre declaring requires a property value supplied by an
> object that belongs to the client, declare the argument with the data type
> of the property rather than the objects class. Marshaling an object
> reference requires significantly more overhead than marshaling a simple data
> type

> there are several techniques you can use to increase the real speed of your
> applications:
> Avoid using Variant variables.
> Use Long integer variables and integer math.
> Cache frequently used properties in variables.
> Replace procedure calls with inline procedures.
> Use constants whenever possible.
> Pass arguments ByVal instead of ByRef.
> Use typed optional arguments.
> Take advantage of collections.



Fri, 25 Jan 2002 03:00:00 GMT  
 ByVal or Byref : performance question ?
On Mon, 9 Aug 1999 15:56:12 +0200, "Jean-Philippe Forti"

Quote:

>Dick,
>You're right, but to get access to good performances, we need to use byref
>with strings and pay attention to not modify the strings during the calls !

This is incorrect without specifying more details.  In passing data,
you *must* consider the context in order to determine how much work is
being done during the process.

In passing data to simple SUBs and FUNCTIONs, what you are saying is
true in general.  Passing ByRef will nearly always be faster.

However, when passing to out of process routines, ByVal will nearly
always be faster.  This is because the data must be passed across the
process boundary.  ByRef parameters must be passed both ways (to the
call and return from the call) whether they are modified or not.
ByVal is passed only one way (to the call, and not returned).

It is best to consider the functional aspects (data protection) and
only look at the performance issues in critical code.  Performance
criteria can change from release to release depending on how the
functionality of data protection or exposure is implemented.

Dan



Fri, 25 Jan 2002 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. ByVal or Byref : performance question ?

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

3. Passing Parameters - A ByRef vs ByVal Question

4. Override ByRef with ByVal

5. ByVal / ByRef

6. byVal vs ByRef

7. Change Standard from ByVal to ByRef

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

9. Peformance in VB.NET - ByRef and ByVal

10. objects byval and byref

11. ByRef And ByVal for Object

12. byval / byref

 

 
Powered by phpBB® Forum Software