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.
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.
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.
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.
As when compiling, the compiler has to check the type anyway I think that it will not take significantly more time to replace the auto by what the compiler found as type.
if you want some further information: C++ 11 auto compile time or runtime?
if you want to know if you should use auto: How much is too much with C++11 auto keyword?