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

Richard D. Moores rdmoores at gmail.com
Tue Nov 23 12:28:51 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". By my tests, it's 4.3% to
9.5% faster, depending on the integer tested - size and whether odd or
even. See the speed testing script and results at
<http://tutoree7.pastebin.com/iragLgDz>.

Dick Moores



http://tutoree7.pastebin.com/iragLgDz


More information about the Tutor mailing list