Re: performance hit using byref or byval?

From: Max (nospam_at_notvalid.com)
Date: 10/26/04


Date: Tue, 26 Oct 2004 15:41:58 -0400

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

"Tom Dacon" <tdacon@community.nospam> wrote in message
news:OZ9rQ33uEHA.3840@TK2MSFTNGP12.phx.gbl...
> 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
>
> "Max" <nospam@notvalid.com> wrote in message
> news:eLElvj3uEHA.3624@TK2MSFTNGP09.phx.gbl...
>> 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
>>
>
>



Relevant Pages

  • Which is better, pass, or not pass by reference?
    ... passing by reference. ... My understanding of passing by reference (putting the & before a variable ... the memory usage for this data... ...
    (comp.lang.php)
  • Re: Which is better, pass, or not pass by reference?
    ... > passing by reference. ... > the memory usage for this data... ... Am I right that passing by reference means ... > my variable data is not duplicated thus using less memory than if I had ...
    (comp.lang.php)
  • Re: Can someone explain ByRef and ByVal for me please!?
    ... With a reference type, the value of a given object is, in fact, a reference. ... Therefore passing it ByVal or ByRef is, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Slow re-use of datatable - Why??
    ... not at class level and pass the datatable to my PopulateRowBlock ... I think what I was concerned about was that by passing the datatable ... You would only need to use ByRef if you were replacing the data table with a new data table in the method. ... A DataTable is a reference type, so what you are passing to the method is the reference to the object, not a copy of the object. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Will this object get destroyed?
    ... VB performs reference counting on ... but doesn't on object references passed ByRef. ... passing 'ByRef' no additional reference is created so the count is not ... 'DestroyJunkByValue cls ...
    (microsoft.public.vb.general.discussion)

Loading