![]() |
|
C++ Optimization: const int or const int& ... |
Ashteth
Member #3,310
March 2003
![]() |
As a general rule, passing by reference is better because the processor needn't spend time allocating additional large sums of memory, but is this true for small primitive data types such as integers? I.E. is it better to generally use "const int" or "const int&" to refer to const integers passed to a function? My initial thought is that it is simply better to use "const int" to avoid forcing the processor do large jump operations, but I am uncertain if this overhead is significant. I realize this is probably nitpicking trying to squeeze an additional few clock ticks out of the CPU, but I am genuinely interested in hearing comments on the pros and cons of passing small, primitve constant datatypes by value/reference.
|
gomez
Member #2,800
October 2002
|
Use int, not int& or const int. It's more efficient as there is no indirection (you are not accessing it through a pointer). However, compilers can supposedly optimise a reference indirection away, so don't take my word for it. |
Surt
Member #273
April 2000
![]() |
Also an int is considerably smaller bytewise, than a reference (pointer), so less data has to be copied. --- |
Bob
Free Market Evangelist
September 2000
![]() |
Quote: Also an int is considerably smaller bytewise, than a reference (pointer), so less data has to be copied. You have no guarantee of that. In fact, on the x86, it is currently not true at all. Quote: but I am uncertain if this overhead is significant. I realize this is probably nitpicking trying to squeeze an additional few clock ticks out of the CPU Don't bother - the function call takes up far more overhead than referencing primitive data types. Concentrate on improving your algorithms instead, and leave the cycle-scrapping to the compilers. -- |
Surt
Member #273
April 2000
![]() |
Oops, I thought a pointer was 8 bytes, but it's only 4, same as int (on x86). --- |
guilt
Member #2,553
July 2002
![]() |
Now ,if a x86 were to work in Real mode ,won't a Karthik Kumar Viswanathan |
Korval
Member #1,538
September 2001
![]() |
I only pass a reference to an 'int' when I explicitly want to change the value. If you don't really want the value of a primitive type changed, passing a 'const &' is somewhat bad form, in terms of code style. |
gillius
Member #119
April 2000
|
Korval, I think that the optimizer would have to do the work for you there, because you can't get any better than working with a 32 bit value. Bob is right though, you can't possibly save that much time. Working with something like a simple integer is like the simplest operation a CPU can do. Anyway, on many compilers, references are implemented as pointers. That means you will have a pointer that refers to an int. In either case, at least 4 bytes will be placed on the stack or in the register or whatever calling method is used. You just can't get any simpler than a 32-bit value on the stack or a register value. You're wasting your time thinking about it. If you are still curious, I would say that if implemented the most directly and nievely possible, there is no way a reference can be faster than a 32-bit integer, but with the optimizer, the difference between an int and a const int& or whatever is probably entirely pointless since the optimizer can throw that out and do what it wants (assuming the semantics is the same), in this case it doesn't matter how you call the function since the optimizer will pick the better way on something so trivial. I've seen MSVC.NET elimiate entire functions from memory and elimiate entire classes and variables. If optimizers are that good now it can certainly handle anything on such a trivial level. Gillius |
Javier Gonzalez
Member #1,559
October 2001
![]() |
One more good reason not to use it: void myfunc(const int &x); myfunc(1004); // <- error, you can't pass a reference to an immediate number
|
Cage
Member #1,277
March 2001
|
Javier Gonzalez: In that case, wouldn't some compilers create a temporary int, thus adding even more overhead? Either way, it's probably best just to use myFunc(int) unless you're planning on modifying the external variable, like everyone else is saying
----- |
Javier Gonzalez
Member #1,559
October 2001
![]() |
Woops, you are right. With const that problems doesn't exist, and it creates a const temp var.
|
|