-5

What would be the performance difference when getting result from return value and from output parameter of a function in c#?

Rajaram Shelar
  • 7,537
  • 24
  • 66
  • 107
  • 1
    There is no measurable performance difference. Voting to close. – Simon Whitehead Jan 18 '14 at 06:27
  • out parameter works as reference of an object. while you pass the value you have to put `out` before the value. and return returns the value from a function.http://www.dotnetperls.com/out this will help you to understand `out` and before asking a question search for that. – Kaushik Jan 18 '14 at 06:29
  • @Simon: There are other reasons to close, but there *is* a performance difference... if you look really really really hard and squint. – JasonS Jan 18 '14 at 06:41

4 Answers4

0

The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. For example:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

Reference of out

Reference 2

Return The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a void type, the return statement can be omitted.

If the return statement is inside a try block, the finally block, if one exists, will be executed before control returns to the calling method.

class ReturnTest
    {
        static double CalculateArea(int r)
        {
            double area = r * r * Math.PI;
            return area;
        }

        static void Main()
        {
            int radius = 5;
            double result = CalculateArea(radius);
            Console.WriteLine("The area is {0:0.00}", result);

            // Keep the console open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    // Output: The area is 78.54

Return Reference

Kaushik
  • 2,072
  • 1
  • 23
  • 31
0

return is lower performance returning constructs a new variable in the parent scope's (method's) stack and that value / reference is copied to it.

out is faster performance out passes to your function a reference to the variable in the parent's scope. so chaning the variable is going to immediatly change the result of the parent's

out vs ref: there is no performance difference between the "out" and "ref" keywords, just a compile-time safety check.

edit: here's an explanation of where it would matter: if you are calling a method a few billion times per second: What is the benefit of using out/ref versus returning? otherwise, just make your code easy to understand.

Community
  • 1
  • 1
JasonS
  • 7,443
  • 5
  • 41
  • 61
  • 1
    PS: PREMATURE OPTIMIZATION IS THE ROOT OF ALL EVIL! don't do the devil's work. – JasonS Jan 18 '14 at 06:37
  • Any sources about `return` being slower then `out`? – MarcinJuraszek Jan 18 '14 at 06:40
  • if you have access to the XNA dll's, go look at the Vector and Matrix override methods. You'll see they favor passing things by ref. it's for this reason. sorry i don't have a link at the moment. (there are some blog posts about this through the years) – JasonS Jan 18 '14 at 06:44
  • i found a SO question re xna, so i linked that in my answer – JasonS Jan 18 '14 at 06:48
0

As I mentioned in the comments, the out parameter performance is described here by @JonSkeet. This is an excerpt from Jon's answer:

"Basically by using an out parameter we're writing the data directly to the final destination, rather than writing it to the small method's stack frame and then copying it back into the Main method's stack frame."

So performance wise, if you're using a small value type, then there isn't any significant performance difference between return and out.

Community
  • 1
  • 1
Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
  • ...it being a small value type makes no difference. A gigantic refetence type copies the reference to the frame.. which is immeasurably fast. – Simon Whitehead Jan 18 '14 at 07:32
-2

Basically you want to be using return if the method has nothing else to return, if then you wish to return something else via a single method you can use out. Performance wise there's no difference.

Which is better, return value or out parameter?

Community
  • 1
  • 1
Spark
  • 1,007
  • 1
  • 8
  • 26