Hi,

I would like to know which one of the following parameter types is faster.
I think is ref because sending a pointer to a class seems to be much more
faster then copy all the class at once in the method call.
But when I was having classes about C# programming in Microsoft Official
Course, my teacher told me that in .NET val is faster, that's why it is the
default parameter type. But he couldn't explain why.
Any ideas?

Thank you.
Roby Eisenbraun Martins

RE: Parameter Methods Performance (val,ref,out) by DavidAnton

DavidAnton
Fri Jun 03 08:56:01 CDT 2005

First of all, passing an object by value does not copy the entire object -
just the reference pointer, so this degree of copying is trivial.

Passing by value is generally faster because the argument is passed one way
- there is no marshalling back to the callee.

However, I've never come across situations where this is a performance
consideration.

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"Roby Eisenbraun Martins" wrote:

> Hi,
>
> I would like to know which one of the following parameter types is faster.
> I think is ref because sending a pointer to a class seems to be much more
> faster then copy all the class at once in the method call.
> But when I was having classes about C# programming in Microsoft Official
> Course, my teacher told me that in .NET val is faster, that's why it is the
> default parameter type. But he couldn't explain why.
> Any ideas?
>
> Thank you.
> Roby Eisenbraun Martins
>

RE: Parameter Methods Performance (val,ref,out) by RobyEisenbraunMartins

RobyEisenbraunMartins
Fri Jun 03 09:05:43 CDT 2005

Thank you David,

Allow me to give you an example:
1) is a method using ref in the method
Process( ref A );
2) is a method that receives a val and return the new variable
A = Process( A );
See, both make exactly the same thing by using differents parameter's type.
Ok, in this case which on is faster?

"David Anton" wrote:

> First of all, passing an object by value does not copy the entire object -
> just the reference pointer, so this degree of copying is trivial.
>
> Passing by value is generally faster because the argument is passed one way
> - there is no marshalling back to the callee.
>
> However, I've never come across situations where this is a performance
> consideration.
>
> David Anton
> www.tangiblesoftwaresolutions.com
> Home of the Instant C# VB.NET to C# converter
> and the Instant VB C# to VB.NET converter
>
> "Roby Eisenbraun Martins" wrote:
>
> > Hi,
> >
> > I would like to know which one of the following parameter types is faster.
> > I think is ref because sending a pointer to a class seems to be much more
> > faster then copy all the class at once in the method call.
> > But when I was having classes about C# programming in Microsoft Official
> > Course, my teacher told me that in .NET val is faster, that's why it is the
> > default parameter type. But he couldn't explain why.
> > Any ideas?
> >
> > Thank you.
> > Roby Eisenbraun Martins
> >

RE: Parameter Methods Performance (val,ref,out) by DavidAnton

DavidAnton
Fri Jun 03 09:07:01 CDT 2005

Interestingly, you would expect passing as 'out' to be roughly the same
performance-wise as by value since you would assume that marshalling is only
occurring in one direction (back to the callee), but at least in debug mode
the argument is marshalled both ways. Not sure if this is a bug or not in
Visual Studio... (you can verify this by noticing that the argument's initial
value is obtainable via the usual debugging tools upon entering the method.)

"David Anton" wrote:

> First of all, passing an object by value does not copy the entire object -
> just the reference pointer, so this degree of copying is trivial.
>
> Passing by value is generally faster because the argument is passed one way
> - there is no marshalling back to the callee.
>
> However, I've never come across situations where this is a performance
> consideration.
>
> David Anton
> www.tangiblesoftwaresolutions.com
> Home of the Instant C# VB.NET to C# converter
> and the Instant VB C# to VB.NET converter
>
> "Roby Eisenbraun Martins" wrote:
>
> > Hi,
> >
> > I would like to know which one of the following parameter types is faster.
> > I think is ref because sending a pointer to a class seems to be much more
> > faster then copy all the class at once in the method call.
> > But when I was having classes about C# programming in Microsoft Official
> > Course, my teacher told me that in .NET val is faster, that's why it is the
> > default parameter type. But he couldn't explain why.
> > Any ideas?
> >
> > Thank you.
> > Roby Eisenbraun Martins
> >

RE: Parameter Methods Performance (val,ref,out) by DavidAnton

DavidAnton
Fri Jun 03 09:20:05 CDT 2005

In this sort of situation, I would just do a contrived test: i.e., construct
two loops, one loop calling a method with a ref param a million times and one
loop calling a function a million times and see which is faster. (probably
very close?)

"Roby Eisenbraun Martins" wrote:

> Thank you David,
>
> Allow me to give you an example:
> 1) is a method using ref in the method
> Process( ref A );
> 2) is a method that receives a val and return the new variable
> A = Process( A );
> See, both make exactly the same thing by using differents parameter's type.
> Ok, in this case which on is faster?
>
> "David Anton" wrote:
>
> > First of all, passing an object by value does not copy the entire object -
> > just the reference pointer, so this degree of copying is trivial.
> >
> > Passing by value is generally faster because the argument is passed one way
> > - there is no marshalling back to the callee.
> >
> > However, I've never come across situations where this is a performance
> > consideration.
> >
> > David Anton
> > www.tangiblesoftwaresolutions.com
> > Home of the Instant C# VB.NET to C# converter
> > and the Instant VB C# to VB.NET converter
> >
> > "Roby Eisenbraun Martins" wrote:
> >
> > > Hi,
> > >
> > > I would like to know which one of the following parameter types is faster.
> > > I think is ref because sending a pointer to a class seems to be much more
> > > faster then copy all the class at once in the method call.
> > > But when I was having classes about C# programming in Microsoft Official
> > > Course, my teacher told me that in .NET val is faster, that's why it is the
> > > default parameter type. But he couldn't explain why.
> > > Any ideas?
> > >
> > > Thank you.
> > > Roby Eisenbraun Martins
> > >

Re: Parameter Methods Performance (val,ref,out) by Jon

Jon
Fri Jun 03 14:45:48 CDT 2005

<"=?Utf-8?B?Um9ieSBFaXNlbmJyYXVuIE1hcnRpbnM=?=" <Roby Eisenbraun
Martins@discussions.microsoft.com>> wrote:
> I would like to know which one of the following parameter types is faster.
> I think is ref because sending a pointer to a class seems to be much more
> faster then copy all the class at once in the method call.
> But when I was having classes about C# programming in Microsoft Official
> Course, my teacher told me that in .NET val is faster, that's why it is the
> default parameter type. But he couldn't explain why.

See http://www.pobox.com/~skeet/csharp/parameters.html

It's very important to understand the difference between passing a
parameter which happens to be a reference by value, and passing a
parameter *by* reference.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too