Recently I was going through a good problem 1538D - Another Problem About Dividing Numbers where I had used long long int which gave me TLE then I switched to int which gave AC. Then I thought of trying out the int_fast32_t which usually pops up as a suggestion in my VS Code whenever I type int but haven't tried it until now. I submitted the same code just by changing long long int to int and then to int_fast32_t and I noticed slight improvement in the execution time when I switched onto int_fast32_t. Here are the submissions using : - long long int : 120003544 -> TLE. - int : 120003641 -> 1637 ms. - int_fast32_t : 120004041 -> 1466 ms.
I googled a lot but haven't understood why and how int_fast32_t is faster or does it just claims to be faster? If it is faster then why very less people use it? Does it have any drawbacks? [I'm bad at googling stuff apologies :( ] I couldn't find any side by side comparison for these. Any helpful links/answers would be helpful! Thanks!
Integer type values take machine words.
Size of machine word is architecture defined.
It is faster to work with many values with machine word size than with many values with half machine word size (some extra cost operations there).
So
fast
usually means "not less than needed size but takes round machine words".For example, on 64bit machines
int_fast32_t
would be justint_64t
.Drawbacks:
1) Would use more memory
2) Implementation defined
I prefer fixed size types (int64_t, int32_t, int16_t), you always know which size values.
Bench
See https://en.cppreference.com/w/cpp/header/cstdint.
int
is at least 16 bits and usually 32 bits.int32_t
is guaranteed to be exactly 32 bits, if supported by the implementation.int_fast32_t
is the fastest integer type that has at least 32 bits.