Is & faster than % when checking for odd numbers?

前端 未结 8 934
鱼传尺愫
鱼传尺愫 2020-11-30 04:13

To check for odd and even integer, is the lowest bit checking more efficient than using the modulo?

>>> def isodd(num):
        return num & 1 a         


        
8条回答
  •  旧巷少年郎
    2020-11-30 04:19

    "return num & 1 and True or False" ? Wah! If you're speed-crazy (1) "return num & 1" (2) inline it: if somenumber % 2 == 1 is legible AND beats isodd(somenumber) because it avoids the Python function call.

提交回复
热议问题