Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does auto in C++ 11 make compile time longer? [duplicate]

auto abc = 5566;

As far as I know, compiler knows abc is int at compile time so it will not affect runtime performance.

However, does it makes compile time longer??

Thanks in advance.

like image 791
Kenny Lee Avatar asked Nov 13 '13 08:11

Kenny Lee


2 Answers

auto is one character longer than int, so the lexer definitely has to do more work.

On the other hand, the compiler no longer has to check that the user provided an appropriate type, so my best guess is that auto will be slightly faster.

In the end, you should probably not decide between type inference and explicit typing based on performance considerations. Intent and clarity should be the deciding factors.

like image 73
fredoverflow Avatar answered Sep 19 '22 03:09

fredoverflow


It may, or it may not, it depends on the compiler. Certainly the performance of this is not something mandated by the standard.

Since the lexical analyser knows the type of 5566 anyway, it's likely to be largely irrelevant.

You'd be better off worrying about more "macro" issues like algorithm and data structure selection. You'll almost certainly get a better return on investment than worry about whether auto type selection is faster or not.

Use of auto in creating variables is not so much about performance as it is about making your life easier as a coder.

like image 34
paxdiablo Avatar answered Sep 23 '22 03:09

paxdiablo