7

A friend told me that it's more efficient to do

int addNumbers(const int number1, const int number2);

than

int addNumbers(int number1, int number2);

assuming of course that number1 and number2 won't be assigned new values. Does this result in a significant performance boost? Are there any other side effects I should know about?

5 Answers 5

12

const correctness is more of letting compiler help you guard against making honest mistakes. Declaring the const-ness of a parameter is just another form of type safety rather than a boost for performance.

Most of the modern compiler will be able to detect if a variable is really constant or not, and apply correct optimizations. So do not use const-correctness for performance reasons. rather use it for maintainability reasons & preventing yourself from doing stupid mistakes.

Sign up to request clarification or add additional context in comments.

4 Comments

I have given up asking the Q why the downvote? but it would be nice to know the counter argument for doing so in this particular case.
Counter argument: less typing. But since everyone here is making a strong case in favor of consting wherever possible to prevent stupid bugs, it's a very meager defense.
@Pieter: "less typing" is never a counter argument when arguing against clarity and bringing across your intent.
Not only letting the compiler know, but also letting YOU (the developer) know. If you have a abstract class that user's need to extend, then having a function 'const correct' will let to developer know you are not to mess around with the particular variable. Obviously, this is better observed in object references...
8

I hope you are aware that in terms of function declarations these two are identical, that is they declare the same function!!!

Now, as far as the definitions go, I can't say if there's any boost at all, but I can promise you there is no significant boost. I don't believe modern compilers are stupid. Most of them are smarter than you and I. ))

There's another side. Some programmers will prefer to add const wherever it's applicable, to be genuinely const-correct. This is a valid point of view and practice. But again, I wouldn't do it just for performance issues.

Comments

0

This would be micro-optimisation at best; wrong at worst.

Don't bother with ideas like this.

If you are not going to change the value, use const. And move on.

Comments

0

You may be thinking of something like this:

void foo(vector<int> large_object)

vs.

void foo(const vector<int>& large_object)

The second case may be faster because the compiler will only push a reference on the stack. In the first case the entire vector will be pushed.

Comments

-1

This probably depends on the compiler and the nature of the function, the there isn't really any room for optimization. If you think about the compiler's job of choosing when to use CPU registers and which load instruction to use, I can not find any use cases where knowing that a parameter is const will allow the compiler to optimize code.

However, you should still use const in your function declarations. It is self documenting and lets others know the nature of the parameters. Also, it might prevent you from doing something unintentionally with a variable:

void addNumbers(const int num1, const int num2)
{
    ...
    num1++;   // you really didn't mean to do this!
}

2 Comments

Indeed. Use const except for when you need to modify something. As opposed to "use const when you don't need to modify something"!
Why should the function declaration determine how the function will be implemented? Say you for some reason want to use one of the parameters as a counter and decrement it in a loop. In that case you have to change the function declaration, and I believe I don't have to explain why that is a bad thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.