11
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.

billz
  • 44,644
  • 9
  • 83
  • 100
Hemingway Lee
  • 759
  • 1
  • 9
  • 21
  • 4
    It might take a nanosecond longer. –  Nov 13 '13 at 08:47
  • 7
    @rightfold or a nanosecond less. – juanchopanza Nov 13 '13 at 08:47
  • The compiler has to know the type of the initializer anyway. – chris Nov 13 '13 at 08:47
  • 2
    Hardware is cheap, programmers ain't. – user1095108 Nov 13 '13 at 08:49
  • i think it will be shorter instead of longer. because it don't need to check type of `abc` is same as `5566` – Bryan Chen Nov 13 '13 at 08:49
  • Most likely it is exactly the same [aside from the extra character of source code to read, which will most likely add several instructions to the execution time of the compiler, but so will adding a comment, or some such - and you can't really tell the difference there, can you?] – Mats Petersson Nov 13 '13 at 08:49
  • 3
    Write a program that emits 1,000,000 auto or typed variables, #include it a time the compilation of each 30x, discard the best and worst of each, average the result and ... have your answer? – kfsone Nov 13 '13 at 08:53
  • @kfsone - OP wanted to know if type deduction by compiler takes some measurable time. Seems pretty logical and also deserved 4 upvotes in duplicate answer. – SChepurin Nov 13 '13 at 08:57
  • MArking it as duplicate. Oh, and yeah, it is a very good/valid question. – SigTerm Nov 13 '13 at 09:13
  • 1
    @schepurin I upvoted, I was suggesting a way to test, because the answer likely varies between compilers and how they implement it. A while back I found that `#include ` was adding 26 seconds per 100 compilation units to a gcc build this way. – kfsone Nov 13 '13 at 18:37

3 Answers3

34

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.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 14
    "`auto` is one character longer than `int`, so the lexer definitely has to do more work." - I believe we have a winner here. – Mysticial Nov 13 '13 at 08:51
  • 8
    What about if `int` is followed by ten spaces but `auto` is only followed by one? :-) Still, damn funny. – paxdiablo Nov 13 '13 at 08:53
  • 2
    If we're worried about the work the lexer has to do, what's in a typical "auto usage case" where the type is something like `std::vector>::const_iterator`? – Damon Nov 13 '13 at 09:43
  • OTOH with auto, the compiler will still have to deduce the type - taking time - and _then_ have to store the initializer for it. I would say `auto` is likely slightly _slower_ – sehe Nov 13 '13 at 20:26
  • 4
    @sehe The compiler already knows the type of the initializer. Otherwise, it couldn't give you compiler errors if you get the type of the variable wrong. – fredoverflow Nov 13 '13 at 20:30
  • @FredOverflow Mmmm. You could be right. If I were writing the compiler, I'd then emit metadata/instructions for the declaration using the same code path, making it a break-even, but yeah, it's likely not going to be longer. – sehe Nov 13 '13 at 20:32
3

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

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?

Community
  • 1
  • 1
Theolodis
  • 4,977
  • 3
  • 34
  • 53