Samuel
Samuel

Reputation: 12341

Performance difference between returning a value directly or creating a temporary variable

Is there any performance hit or memory consumption difference to creating a temporary variable in a function compared to returning directly the value assigned to this variable?

For example, which of these functions (GetValue) are better in performance and for saving memory or both are exactly the same:

Case 1:

  private string GetValue()
  {
     return this.GetResult();
  }

  private string GetResult()
  {
     // Code here that return a big string...
  }

Case 2:

  private string GetValue()
  {
     string result = this.GetResult();

     return result;
  }

  private string GetResult()
  {
     // Code here that return a big string...
  }

Thank you.

Upvotes: 20

Views: 4103

Answers (3)

ihimv
ihimv

Reputation: 1474

The local variable is always optimised.

There is no performance impact of using a local variable before a return statement since the Compiler optimizes the code.

So, after the compilation, the Case 2 code will look exactly like Case 1.

  private string GetValue()
  {
     // string result = this.GetResult(); // this is optimized to the below line by compiler

     return this.GetResult();
  }

  private string GetResult()
  {
     // Code here that return a big string...
  }

See here for the actual output generated by the Compiler when we use a variable and when we don't before returning a result.

I prefer using the local variable always as it speeds up debugging. According to this, developers spend 75% of their time debugging.

UPDATE: As pointed out by @surfmuggle in the comments, there could be a slight impact (two additional lines of IL code) of using a local variable. But, as said in the accepted answer, readability takes preference over such nano-optimizations.

Upvotes: 2

Adam Houldsworth
Adam Houldsworth

Reputation: 64477

In these basic situations, readability always trumps performance differences. I'd consider this a micro-optimisation at best, and these largely turn out to be wastes of time. What you save on this will be eaten up by an undeterministic GC run.

Most of the time there are no differences in the resulting code if the compiler is allowed to optimise it. The resulting IL in this case seems to have a few extra op codes for a reference to the string on the stack, but what the JIT then does with this is anyone's guess.

I sometimes break out into temporary variables to review them before returning, but I never worry about the performance impact. Most importantly, I have never seen a case where this sort of improvement was required to solve a performance problem.

Upvotes: 20

Guffa
Guffa

Reputation: 700252

If the local variable is actually used by the executable code, and not optmised away, then the difference is still minimal.

The local variable uses just the stack space needed to store the reference, and allocating the space for it takes no time at all as the stack frame is always allocated anyway.

The time to make the extra copy to and from the local variable would hardly be possible to measure. It would only make a difference if you would call the method millions of times in a tight loop, and it would still only be a tiny fraction of the execution time compared to the time it takes to allocate a string.

Upvotes: 5

Related Questions