0

Is there any valuable performance difference between this:

int myFunction(const int &a) { return 2 + a; }

and this:

int myFunction(int a) { return 2 + a; }

?

As far as I know, the second method makes a copy of the variable passed as argument to a new register, and this takes some more instructions in the asm code. That this function is called thousand times a second and performance is crucial in this case, is it better to pass the const reference rather than the variable?

Mark Miles
  • 706
  • 8
  • 20
  • Have you looked this up? You are not the first one to ask this – Samuel Jul 07 '14 at 13:43
  • _'Is there any valuable performance difference between this:'_ Very unlikely. – πάντα ῥεῖ Jul 07 '14 at 13:44
  • In reality a good optimizing compiler will probably inline both functions as written. Otherwise, passing by reference is generally implemented using pointers, which are not smaller than `int`s (and actually larger in 64-bit systems). – T.C. Jul 07 '14 at 13:48
  • Bjarne says for built in types use pass by value. – 101010 Jul 07 '14 at 13:48
  • 1
    If performance is crucial, you better performancetest the your product, compiled with optimization flags on. If myFunction is indeed a bottleneck, you could look at the resulting machine code and you might see that both versions result in the same inlined code. – Maarten Hilferink Jul 07 '14 at 14:03

3 Answers3

5

If your compiler can't see that these two bits of code have identical effects, it's a piece of junk and you shouldn't use it.

But your argument is nonsense. Here's what a naive implementation of the two would do:

1: int myFunction(const int &a) { return 2 + a; }
Pass the address of a. Get the value from that address. Add two to it. Return it.

2: int myFunction(int a) { return 2 + a; }
Pass a's value. Add two to it. Return it.

Notice the second one avoids two operations -- getting a's address and then getting a's value from that address. Worse, the first one only works if a is stored in memory, which means it will have to be stored in memory if it's not already there. Ouch.

For a mere thousand calls a second, it's not going to make much difference. But for the more complicated cases where the compiler may not be able to see everything at once, it's standard practice to pass "small" types by value. You only want to pass by reference when the cost of the extra indirection is less than the cost of copying the value.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • "If your compiler can't see that these two bits of code have identical effects, it's a piece of junk and you shouldn't use it." Assuming compiler can see the function body at call point. It might not be able to in real code. – Neil Kirk Jul 07 '14 at 13:57
  • @NeilKirk That's right. For more complicated cases, it can definitely matter which of these two things you do. – David Schwartz Jul 07 '14 at 13:57
  • +1 dude this answer is much better, even from the answers in the duplicate. – 101010 Jul 07 '14 at 14:15
2

For small types passing them by value can be more effective than passing by const reference, because references add a level of indirection.

For the definition of small type here, you should measure, but for values around the word size of your platform (e.g. ints, floats) you are better off passing by value.

Zsolt
  • 582
  • 2
  • 5
1

Usually the second function definition is more efficient than the first because in the first function definition there is used indirect reference to the argument.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335