[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 17:33:47 CET 2010
On Tue, Nov 23, 2010 at 08:22, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "Richard D. Moores" <rdmoores at gmail.com> wrote
>
>> 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>.
>
> Did you try:
>
> if n % 2: pass
> else: do it here?
>
> ie inverting the logic and omitting the equality test.
Alan, since I'm doing comparison's, why do you suggest omitting the
equality test?
Also, what do you mean by "do it here?"
Dick
More information about the Tutor
mailing list