Why would uint32_t be preferred rather than uint_fast32_t?

前端 未结 11 1175
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 00:58

It seems that uint32_t is much more prevalent than uint_fast32_t (I realise this is anecdotal evidence). That seems counter-intuitive to me, though.

11条回答
  •  攒了一身酷
    2021-01-31 01:49

    uint32_t is guaranteed to have nearly the same properties on any platform that supports it.1

    uint_fast32_t has very little guarantees about how it behaves on different systems in comparison.

    If you switch to a platform where uint_fast32_t has a different size, all code that uses uint_fast32_t has to be retested and validated. All stability assumptions are going to be out the window. The entire system is going to work differently.

    When writing your code, you may not even have access to a uint_fast32_t system that isn't 32 bits in size.

    uint32_t won't work differently (see footnote).

    Correctness is more important than speed. Premature correctness is thus a better plan than premature optimization.

    In the event I was writing code for systems where uint_fast32_t was 64 or more bits, I might test my code for both cases and use it. Barring both need and opportunity, doing so is a bad plan.

    Finally, uint_fast32_t when you are storing it for any length of time or number of instances can be slower than uint32 simply due to cache size issues and memory bandwidth. Todays computers are far more often memory-bound than CPU bound, and uint_fast32_t could be faster in isolation but not after you account for memory overhead.


    1 As @chux has noted in a comment, if unsigned is larger than uint32_t, arithmetic on uint32_t goes through the usual integer promotions, and if not, it stays as uint32_t. This can cause bugs. Nothing is ever perfect.

提交回复
热议问题