[Tutor] "if n % 2 == 0" vs. "if not n % 2" compared for speed: aesthetics lose

R. Alan Monroe amonroe at columbus.rr.com
Tue Nov 23 19:29:45 CET 2010


> I've always disliked using "if not n % 2"  to test for even/odd ints
> because of its convoluted logic. But I ran some speed tests and found
> it was the way to go over "if n % 2 == 0".

Did you try bitwise-and with 1?

Alan



More information about the Tutor mailing list