Switch optimization for many cases guarantees equal access time for any case? ( C++ )

前端 未结 6 1344
礼貌的吻别
礼貌的吻别 2020-12-31 19:29

I\'ve seen answers here for specific languages, about switches with more than 5 cases being optimized with jump tables to guarantee constant access time for any case.
Is

6条回答
  •  渐次进展
    2020-12-31 20:12

    c (and by extension c++) only switches on integer types, so hashing is not necessary. The compiler will typically use an idiom appropriate to the architecture you're compiling for. This could be indexed addressing (if a small range is used), jump tables, or something entirely different.

提交回复
热议问题