which is faster to find the even number if(n%2==0) or if(n&1==0)? [duplicate]
问题 This question already has answers here : Is & faster than % when checking for odd numbers? (8 answers) Closed 2 years ago . which is faster to find the even number if(n%2==0) or if(n&1==0) ? def isEven(n): if n%2 == 0: return True return False OR def isEven(n): if n&1 == 1: return False return True 回答1: This looks like Python, so I tried it in Python(3.6): from timeit import timeit a = timeit(stmt='for i in range(100): i%2') b = timeit(stmt='for i in range(100): i&1') print(a, b) Times vary