Re: Passing an object reference 1000 times

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 11/12/04


Date: Fri, 12 Nov 2004 09:30:32 -0500

Dennis,

    Like I said before, I don't think that this is the issue. I think that
if you ran some performance tests between the two, you wouldn't find any
difference.

-- 
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com
"Dennis Myrén" <dennis@oslokb.no> wrote in message 
news:sJ3ld.16330$Km6.175390@news4.e.nsc.no...
> Thank you for answering, Nicholas.
>
> Actually, i do know i am passing a *reference* to the object; a 32-bit 
> integer.
> However, it still a lot of 32-bit integers.
> So, you think i should go for passing by value rather than by 
> reference(ref)?
>
> -- 
> Regards,
> Dennis JD Myrén
> Oslo Kodebureau
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote 
> in message news:Oy$mfKMyEHA.3376@TK2MSFTNGP12.phx.gbl...
>> Dennis,
>>
>>    I think that the assumption you are making is that when you are 
>> passing a reference type around, you are copying it as you make the call 
>> passing the object.  This is not the case.  You are passing a reference, 
>> which is very small compared to the actual object size.
>>
>>    If you are feeling slowdown, then it is most likely because of code IN 
>> the method, and not how you are passing the parameters.  If you were to 
>> take some performance numbers between passing it with and without the ref 
>> keyword, I think you would find they are pretty much exactly the same.
>>
>>    Hope this helps.
>>
>>
>> -- 
>>               - Nicholas Paldino [.NET/C# MVP]
>>               - mvp@spam.guard.caspershouse.com
>>
>> "Dennis Myrén" <dennis@oslokb.no> wrote in message 
>> news:ik3ld.16323$Km6.175314@news4.e.nsc.no...
>>>I have these tiny classes, implementing an interface through which their 
>>>method
>>> Render ( CosWriter writer ) ;
>>> is called.
>>> Given a specific context, there are potentially a lot of such objects, 
>>> each requiring a call to that method to fulfill their purpose.
>>> There could be 200, there could be more than 1000.
>>> That is a lot of references passed around. It feels heavy.
>>>
>>> Let us say i changed the signature of the interface method to:
>>> Render ( ref CosWriter writer ) ;
>>> to pass an existing reference to a CosWriter instance(CosWriter is a 
>>> class).
>>>
>>> Because the ref keyword is thought to be used to actually change that 
>>> parameter in the method to which it was passed,
>>> would it be considered bad practice, passing an instance by reference, 
>>> although i will not change that parameter in the method?
>>>
>>> I am not very keen on passing this instance by value that much, when i 
>>> can use the same reference.
>>> I would earn a lot in both memory allocation and speed, would i not?
>>>
>>> The only option i have to the Render method implementation on each of 
>>> this objects,
>>> is i could check what type each one is, cast to that type to be able to 
>>> access a specific property
>>> of it, and perform what otherwise that method Render does, suppressing 
>>> that method.
>>> Could that be more efficient? I doubt.
>>>
>>> Those properties is mostly System.Int32, in some cases they are 
>>> reference types.
>>> So i would not earn anything from that, would i?
>>>
>>>
>>> Please give me some guidelines, and i am very grateful.
>>>
>>>
>>>
>>> -- 
>>> Regards,
>>> Dennis JD Myrén
>>> Oslo Kodebureau
>>>
>>
>>
>
> 


Relevant Pages

  • Re: pointer syntax
    ... > really passing a reference (which is like an old-school pointer, ... > a copy of the primitive's value and passing that. ... build them yourself with pointers. ... mean a var parameter, write a var parameter. ...
    (comp.lang.pascal.delphi.misc)
  • 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: Passing by reference
    ... It depends upon the reason one is passing the reference. ... instantiate all relationships via a constructor so there will always be ... I naturally think of 'getter' methods as 'knowledge ...
    (comp.object)
  • Re: Using ref
    ... the address of a variable that contains a reference to an object, ... the C version is passing the address of a variable that contains the   ... Inasmuch as the specification is unambiguous, there is a single truth. ... Every time he states the incorrect view, I will feel compelled to correct his incorrect statements. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ByVal vs. ByRef
    ... When you pass a reference type byval: ... A copy of the passed *pointer* to the reference is made. ... So, if you pass an object ByVal, you're passing that "pointer" ByVal, so ...
    (microsoft.public.dotnet.languages.vb)