When is assembly faster than C?

前端 未结 30 2487
挽巷
挽巷 2020-12-02 03:18

One of the stated reasons for knowing assembler is that, on occasion, it can be employed to write code that will be more performant than writing that code in a higher-level

30条回答
  •  伪装坚强ぢ
    2020-12-02 03:45

    More often than you think, C needs to do things that seem to be unneccessary from an Assembly coder's point of view just because the C standards say so.

    Integer promotion, for example. If you want to shift a char variable in C, one would usually expect that the code would do in fact just that, a single bit shift.

    The standards, however, enforce the compiler to do a sign extend to int before the shift and truncate the result to char afterwards which might complicate code depending on the target processor's architecture.

提交回复
热议问题