0

I just read this link: The difference of int8_t, int_least8_t and int_fast8_t? and now I know that int8_t is exactly 8 bits whereas int_fast8_t is the fastest int type that has at least 8 bits.

I'm a developer who develops backend processes with c++11 on Linux. Most of time I don't need to worry about the size of my processes. But I need always care about the sizes of integers in my project. For example, if I want to use an int to store the ID of user or to store a millisecond-timepoint, I can't simply use int because it may cause overflow, I must use int32_t or int64_t.

So I'm thinking if it's good to use int_fast8_t everywhere and stop using int8_t (same as int_fast32_t, int_fast64_t, uint_fast8_t etc).

Well, using int_fastN_t may change nothing because my program is always deployed on X86 or Arm64. But I still want to know if there is any drawback if I change all of intN_t into int_fastN_t. If there isn't any drawback, I think I would start to use int_fastN_t and stop using intN_t.

Yves
  • 11,597
  • 17
  • 83
  • 180
  • If your requirement is an integer that is at least N bits but you don't specifically care if it has more, then that is what `int_fastN_t` is for. Use `intN_t` when the extra size would be problematic. Also note that an implementation is not required to provide every `intN_t` types (in cases where such an integer type doesn't exist on that platform) but it is required to provide the `int_fastN_t` types, so it is also more portable. – François Andrieux Sep 01 '21 at 03:13
  • Have you profiled these against one another on your target platforms? Is the integer representation _really_ a measurable performance bottleneck for your programs? – paddy Sep 01 '21 at 03:19
  • @FrançoisAndrieux lol, in the projects of my company, there isn't any `int`, we always use `int8_t`, `int32_t` etc... That's why I pose this question. – Yves Sep 01 '21 at 03:23
  • @paddy No. In fact, my idea was quite simple: lots of int_N were used in my projects, since int_N is not guarnteed to be provided, I was thinking int_fastN_t maybe a better choice... – Yves Sep 01 '21 at 03:39

1 Answers1

0

So I'm thinking if it's good to use int_fast8_t everywhere

No. It's not good to use it everywhere.

I still want to know if there is any drawback if I change all of intN_t into int_fastN_t

The main drawback is that the size of the integer won't be exactly N bits. In some use cases, this is crucial.

Another potential drawback is that it may be slower. Yes, "fast" type alias can be slower. The alias isn't magic; the efficiency depends on use case.

Using the "fast" alias is fine if:

  • The exact size doesn't matter, but only the minimum.
  • You have the option of changing the type later (no need for backward compatibility).
  • You don't have time to measure which type is actually fastest (which is often reasonable).

You didn't ask for drawbacks of using the fixed width integers. But for balance, I'll mention: they are not guaranteed to be provided on all systems. And of course, they may be slower in some other use cases (which is probably less surprising given the naming).

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • In fact, my idea was quite simple: lots of `int_N` were used in my projects, since `int_N` is not guarnteed to be provided, I was thinking `int_fastN_t` maybe a better choice... – Yves Sep 01 '21 at 03:35
  • Well, if `int_fastN_t` has these drawbacks, I won't touch them. – Yves Sep 01 '21 at 03:36
  • Also, you do not think the data will participate in any memory pressure problems. Arrays of (or data containing) `int_fast8_t` can easily use up more memory than `int8_t` and the resulting extra memory usage beat out any *local* speedup when doing math on them. – Yakk - Adam Nevraumont Sep 01 '21 at 20:55