which test faster : equality or inequality ?
for example, in big while
loop, should condition a>0
rather a!=0
?
when asking question speed differences between different operators, use timeit
module measure. equally fast:
>>> import timeit >>> timeit.timeit('a > 0', 'a = 1', number=10**7) 0.2486400604248047 >>> timeit.timeit('a > 0', 'a = 0', number=10**7) 0.2411360740661621 >>> timeit.timeit('a != 0', 'a = 1', number=10**7) 0.24765801429748535 >>> timeit.timeit('a != 0', 'a = 0', number=10**7) 0.24990510940551758
that's comparisons repeated 10 million times, , if re-run above tests you'll find timings can vary , none clear winners.
you should focusing on readability here, not speed. simple integer comparison going infinitesimal part of overall execution speed, in loop.
Comments
Post a Comment