[Tutor] Find out if a number is even or not

Bill Mill bill.mill at gmail.com
Mon Oct 18 22:05:07 CEST 2004


Alan,

this approach (return n%2 and False or True, labelled isEven4) appears
to be the slowest:

isEven1: 7.345000
isEven2: 7.811000
isEven3: 8.685000
isEven4: 9.129000

Why these times are, on average, faster than they were before, I'm not
sure. Still, the boolean type approach is the slowest. Perhaps there
is some overhead in the creation of booleans?

Peace
Bill Mill
bill.mill at gmail.com


On Sun, 17 Oct 2004 08:03:57 +0100, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> > def isEven1(n): return not n&1
> > def isEven2(n): return n % 2 == 0
> > def isEven3(n): return n % 2 and 'Odd' or 'Even'
> 
> Since the last is doing something quite different
> could you try:
> 
> def isEven4(n): return n%2 and False or True
> 
> Which makes it closer to the others. I'd still expect it to
> be slower but I wonder whether it closes the gap in any way?
> 
> Alan g
>


More information about the Tutor mailing list