Parameter Methods Performance (val,ref,out)

G

Guest

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
 
G

Guest

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
 
G

Guest

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?
 
G

Guest

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.)
 
G

Guest

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?)
 
J

Jon Skeet [C# MVP]

<"=?Utf-8?B?Um9ieSBFaXNlbmJyYXVuIE1hcnRpbnM=?=" <Roby Eisenbraun
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top