Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2004

Tip: Looking for answers? Try searching our database.

performance hit using byref or byval?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Max - 26 Oct 2004 17:29 GMT
New to winforms, I'm trying to break up my code to not have so much in the
main form, so I'm moving code outside into reusable code. My concern is that
I'm passing huge objects like entire datagrids BYREF to modify and molest in
my functions. Are there any performance issues I need to worry about? I want
my app to use as little memory and cpu resources as possible.

-Max
Tom Dacon - 26 Oct 2004 18:04 GMT
You're not actually passing the huge object around when you use byref. All
you're passing is the reference to the object, which resides on the heap.
This is a bit subtle, but this is how it works:

When you pass a reference to an object by value, the receiving method
receives a copy of the reference, guaranteeing that when the method returns
to the calling code that the reference will be unchanged (i.e., will not
point to a different object on the heap).

When you pass a reference to an object by reference, the receiving method is
in essence operating directly on the caller's variable, and could in
principle change it so that it points to some new object of the same type.

In either case, the cost of passing the object reference is very slight, so
you should make your choice about passing convention on other
considerations, such as whether you want to protect the variable's value
from the called method, or whether the called method is authorized to change
it.

These rules apply to objects created on the heap. Objects created on the
stack (structs/Types), I believe, have other considerations.

HTH,
Tom Dacon
Dacon Software Consulting

> New to winforms, I'm trying to break up my code to not have so much in the
> main form, so I'm moving code outside into reusable code. My concern is
[quoted text clipped - 4 lines]
>
> -Max
Max - 26 Oct 2004 20:41 GMT
Ok that puts my mind at ease. I guess what I was reading is that byref
causes more roundtrips in terms of speed rather than the memory usage a
byval might cause. Byval might be faster if I don't mind the memory
overhead. I suppose the issue is insignificant as long as I'm not byrefing a
datagrid a million times in a huge loop or something and expecting fast
results.

-Max

> You're not actually passing the huge object around when you use byref. All
> you're passing is the reference to the object, which resides on the heap.
[quoted text clipped - 30 lines]
>>
>> -Max
Herfried K. Wagner [MVP] - 26 Oct 2004 21:42 GMT
"Max" <nospam@notvalid.com> schrieb:
> Ok that puts my mind at ease. I guess what I was reading is that byref
> causes more roundtrips in terms of speed rather than the memory usage a
> byval might cause. Byval might be faster if I don't mind the memory
> overhead. I suppose the issue is insignificant as long as I'm not byrefing
> a datagrid a million times in a huge loop or something and expecting fast
> results.

I would change the 'ByRef' to 'ByVal' in your particular case.

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/

Tom Dacon - 26 Oct 2004 23:53 GMT
The main point is that the object itself sits quietly on the heap and
doesn't get copied or moved in either case. Just the references get passed
around.

An argument passed by reference probably causes a couple of extra IL
instructions per call, to copy the value of the reference back into the
caller's variable. Compile a small example and look at the IL code in ILDASM
to see the details.

Making a million calls to anything in a huge loop would cause a noticeable
effect, I suppose, even if you didn't pass any arguments at all. The
difference between byval and byref would probably not be noticeable even in
that case. My preference is to always pass input arguments by value except
in extraordinary circumstances, and I can't even remember the last time I
had to do it.

Here are some good rules:
   Use BYVAL if you're supplying an input value to the method that the
called method will either only look at, or change some property of
   Use BYREF if you're supplying an initial value to the method, and the
method is allowed to change the reference to point to a new object
   Use OUT if you don't want to supply an initial value to the method, but
the method will return a reference to a new object in the argument variable

For VB.Net, the keywords are ByVal and ByRef; I don't think VB.Net has the
concept of an output-only parameter but I may be wrong;
For C#, the keywords are 'ref' for passing by reference, and 'out' for
passing for output; passing by value is the default and there's no keyword
for it.

Tom Dacon
Dacon Software Consulting

> Ok that puts my mind at ease. I guess what I was reading is that byref
> causes more roundtrips in terms of speed rather than the memory usage a
[quoted text clipped - 40 lines]
>>>
>>> -Max

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.