0

Sometimes, I got helper methods like:

private string generateReport(string doc, List<string> sheets, Blah blahblah)
{
    //doStuff()
}

Seeing a lot of this in my code makes me wonder if I'm messing with the computer memory - throwing parameters to and fro. On a basic level, this problem is solved by having class level variables but in some cases, I may be deriving from a class or using a helper method from another class. So my question is:

Is using references a preferable option? i.e.

private string generateReport(ref string doc, ref List<string> sheets, ref Blah blahblah)
{
    //doStuff()
}

and when should I not use it (if applicable)?

Thanks.

rtuner
  • 2,362
  • 3
  • 25
  • 37
  • 6
    What gets you the chocolates faster: I hand you a box of chocolates, you open the box and eat the chocolates, or I hand you a piece of paper that contains instructions on how to find a box of chocolates, you follow the instructions, find the box and eat the chocolates? There's no reason to suppose that *adding indirection* makes operations *faster*. Adding a level of indirection makes operations slower; maybe up to a few nanoseconds. – Eric Lippert May 14 '13 at 05:35
  • @EricLippert: Well if it's a BIG box of chocolates then it might be faster for me to just go find it instead of having you bring it... though I would of course still appreciate it if you did the latter ;) – user541686 May 14 '13 at 05:38
  • 2
    @Mehrdad: Right; but that's why structs are supposed to be small. – Eric Lippert May 14 '13 at 05:39
  • 2
    @EricLippert: Yup... then a [`Matrix`](http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.matrix.aspx) struct comes along and ruins the party. :( – user541686 May 14 '13 at 05:41
  • From [here](http://stackoverflow.com/questions/408101/which-is-faster-byval-or-byref), it concludes that passing ByRef may be more suitable for passing reference types. So I think it's not really like this box of chocolate stuff – rtuner May 14 '13 at 05:47
  • @rtuner: Uhm no, ByRef is more useful for passing **value types**. Passing a reference by reference is kind of useless if you don't intend to modify the reference, that's what my answer was trying to say... – user541686 May 14 '13 at 05:47
  • Hmmmm, so you disagree with the conclusion of the answer? http://stackoverflow.com/a/408131/1261657 – rtuner May 14 '13 at 05:52
  • 1
    @rtuner: Nope, I'm saying the same thing as that answer -- ByVal is usually faster for both reference and value types. ByRef is only faster for some value types, and never for reference types. – user541686 May 14 '13 at 05:55

2 Answers2

6

The direct answer is no. Please see this question and the answer: Which is faster? ByVal or ByRef?

Also remember that passing by reference should foremost be a design choice. Passing by reference states that any changes made to the parameters will directly influence the variables that was supplied. This should only be used if you want to return more than one parameter of if you are looking for the above behavior specifically.

And finally, if your code is not processing heavy you should not focus on making very small performance improvements but rather focus on readability and stability.

Community
  • 1
  • 1
Flipbed
  • 719
  • 9
  • 18
1

No -- "passing by reference" is really going to pass a reference, which is 4 bytes on 32-bit systems.

So, if your variable is as big as (or smaller than) a reference, then it's definitely not faster -- likely a little bit slower.

Otherwise, it just depend on how big your variable is -- it could be either faster or slower depending on the code.

user541686
  • 205,094
  • 128
  • 528
  • 886