Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does const call by reference improve performance when applied to primitive types?

Concerning objects (especially strings), call by reference is faster than call-by-value because the function call does not need to create a copy of the original object. Using const, one can also ensure that the reference is not abused.

My question is whether const call-by-reference is also faster if using primitive types, like bool, int or double.

void doSomething(const string & strInput, unsigned int iMode);
void doSomething(const string & strInput, const unsigned int & iMode);

My suspicion is that it is advantageous to use call-by-reference as soon as the primitive type's size in bytes exceeds the size of the address value. Even if the difference is small, I'd like to take the advantage because I call some of these functions quite often.

Additional question: Does inlining have an influence on the answer to my question?

like image 700
Fabian Avatar asked Mar 19 '15 10:03

Fabian


People also ask

Does using const improve performance?

const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic , meaning the compiler has to respect changes made by other threads.

Should I always use const reference?

Yes, you should use const whenever possible. It makes a contract that your code will not change something. Remember, a non-const variable can be passed in to a function that accepts a const parameter. You can always add const, but not take it away (not without a const cast which is a really bad idea).

What is one benefit of declaring the parameter as a const reference?

What is one benefit of declaring the parameter as a const reference instead of declaring it as a regular object? Actually, objects cannot be passed as regular variables, because they require a constructor call. Therefore, a const reference is the only way to pass class instances to functions.

Does const speed up code?

No, const does not help the compiler make faster code.


1 Answers

My suspicion is that it is advantageous to use call-by-reference as soon as the primitive type's size in bytes exceeds the size of the address value. Even if the difference is small, I'd like to take the advantage because I call some of these functions quite often.

Performance tweaking based on hunches works about 0% of the time in C++ (that's is a gut feeling I have about statistics, it works usually...)

It is correct that the const T& will be smaller than the T if sizeof(T) > sizeof(ptr), so usually 32-bits, or 64, depending on the system..

Now ask yourself :

1) How many built-in types are bigger than 64 bits ?

2) Is not copying 32-bits worth making the code less clear ? If your function becomes significantly faster because you didn't copy a 32bit value to it, maybe it doesn't do much ?

3) Are you really that clever ? (spoiler alert : no.) See this great answer for the reason why it is almost always a bad idea : https://stackoverflow.com/a/4705871/1098041

Ultimately just pass by value. If after (thorough) profiling you identify that some function is a bottleneck, and all of the other optimizations that you tried weren't enough (and you should try most of them before this), pass-by-const-reference.

Then See that it doesn't change anything. roll-over and cry.

like image 127
Félix Cantournet Avatar answered Sep 19 '22 14:09

Félix Cantournet