0

Is there anything to be gained by using const int &var formal parameters in place of const int var?

I know the advantage in large structs but for PODs the pointer is often of equal size to the actual data. So I guess the answer would be "No advantage". But I think perhaps the pass by value semantics copies once to the stack and again to an area off the stack for easier reference (say to a register). Or maybe not. In fact it may infact be better to pass by value,

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Because references are typically implemented by C++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values passed by reference is slower than accessing values passed by value.

But this gets back to what I was saying about copying back off the stack. I'm just being paranoid right? This behaviour is for the compiler to worry about and not me.

I could single step the assembly I know, but, well, googling is faster and it came up blank.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
John
  • 6,433
  • 7
  • 47
  • 82
  • 1
    In the end, it's probably too minor of a difference to be worth optimizing. – Dawson Aug 24 '11 at 22:41
  • @Toolbox, I don't know, complex apps on mobile phones seem to be quite popular. – John Aug 24 '11 at 23:13
  • 2
    That complexity is exactly what makes me think there are dozens of other optimizations out of which you'll get much more of a performance increase. – Dawson Aug 24 '11 at 23:25
  • Maybe so, but at the moment I am re-writing this particular class in the hope to give it up for use and abuse by an AI algorithm. – John Aug 24 '11 at 23:27

2 Answers2

3

Consider this:

Non-reference

  • Allocate an int on the stack
  • Copy the value to the stack
  • Call the function like usual
  • Use the variable on the stack

Reference

  • Allocate a pointer on the stack
  • Copy address of int to stack
  • Call the function like usual
  • Dereference the variable on the stack (possibly multiple times in the same function)
  • Use the variable

It's slightly slower to use the reference version. Generally though, let the compiler worry about it. If it's a read-only value, just give it a regular old int. If you need to modify the caller's value, use a reference or a pointer.

Of course that's only true for a small data type like an int. If you were talking about something bigger like a struct or something it would probably be faster to use a reference.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • 2
    A pointer isn't often bigger than an int on 32 bit machines AFAIK. – Seth Carnegie Aug 24 '11 at 22:46
  • I don't think pointers are bigger than ints on most 32bit processors. http://stackoverflow.com/questions/589575/size-of-int-long-etc/589685#589685 – Mooing Duck Aug 24 '11 at 22:51
  • @Seth Carnegie Sure there isn't much of a difference in size (goodness, maybe 4bytes wasted on the stack! and the operation won't be any slower though) but at best it's just as fast and at worst it's slower.. so why do it? – Voo Aug 24 '11 at 22:51
  • @Voo when did I say anything about whether you should or shouldn't do it? I was pointing out that the sizes of pointers and ints are quite often the same on 32 bit machines. That's all. – Seth Carnegie Aug 24 '11 at 23:00
  • "Dereference the variable on the stack (possibly multiple times in the same function)" Surely the compiler will just optimize away this step as most machine instructions have an equivalent with an addessing mode that accepts a reference directly. But you are right, this extra fetch costs nanoseconds. – John Aug 24 '11 at 23:18
  • @Seth Carnegie Sorry if you misunderstood me. The last sentence was purely rhetorical. – Voo Aug 25 '11 at 00:36
1

When you pass a reference you copy the value of the reference, all the same. When you use it, you waste a few cycles on indirection, but other than that there is no performance difference.

Of course, there could be a difference in program behaviour, if you cast away constness, or if you change the referenced value on a different thread.

Don Reba
  • 13,814
  • 3
  • 48
  • 61
  • Just doing basic arithmetic, only just noticed they were still using plain old (int var) as fp's. – John Aug 24 '11 at 23:14