Trivial performance questions

B

Bryan

If you *get into the habit* of always checking with "if x is None:"
rather than "if x == None:" -- two equally readable constructs -- it
will cost you no increase in effort whatsoever to always use the
idiom you've gotten used to. So, whether the (tiny) extra speed and
readability are important or not, it's still a good habit to pick up.
>
Alex

can you explain in more detail why "if x is None:" is better/faster than "if x == None:"? i guess i don't fully
understand "is". my fingers always seem to want to type "if not x:", but that is probably worse still since it includes
(), [], {}, '', False, None .

thanks,

bryan
 
A

Alex Martelli

Bryan said:
If you *get into the habit* of always checking with "if x is None:"
rather than "if x == None:" -- two equally readable constructs -- it
will cost you no increase in effort whatsoever to always use the
idiom you've gotten used to. So, whether the (tiny) extra speed and
readability are important or not, it's still a good habit to pick up.

Alex

can you explain in more detail why "if x is None:" is better/faster than
"if x == None:"? i guess i don't fully
understand "is". my fingers always seem to want to type "if not x:", but
that is probably worse still since it includes (), [], {}, '', False, None

If you WANT to include other false values, then of course "if not x:" is
just perfect. But with either 'is None' or '== None' you're checking
specifically for None -- quite different semantics.

"a is None" a bit more readable than "a == None" because it uses readable
words rather than punctuation. It's a bit faster, because 'is' gets the
id (machine addresses) of its operands and just compares them -- it looks
for IDENTITY, the SAME objects, as opposed to two separate objects which
happen to have the same value; '==' necessarily must do a bit more work,
because many objects can be equal without being the same object.


Alex
 
T

Terry Reedy

Bryan said:
can you explain in more detail why "if x is None:" is better/faster
than "if x == None:"? i guess i don't fully understand "is".

'is' means same object (same identity) For CPython, this is a trivial
comparison of addresses. == means same value, which comparison
starts, I believe, with calling type(x) and type(None) to see if they
are comparable.
my fingers always seem to want to type "if not x:", but that is
probably worse still since it includes (), [], {}, '', False, None .

It is good or erroneous depending on whether 'not x' is or is not the
appropriate condition ;-)

Terry J. Reedy
 
A

Aahz

can you explain in more detail why "if x is None:" is better/faster
than "if x == None:"? i guess i don't fully understand "is". my fingers
always seem to want to type "if not x:", but that is probably worse
still since it includes (), [], {}, '', False, None .

When you want to check whether a target specifically points to the None
object, you can't use == because it calls a Python method that might
return None. That's in addition to the fact that ``is`` is faster.
 
M

Michael Hudson

Alex Martelli said:
Peter Hansen wrote:
...

My, but that timbot guy IS good. Envy, envy...

a) I'm pretty sure timeit.py was written by Guido (he's not bad, either :)

b) Revision 1.6 has this checkin comment:

Broke down and made it work for Python 2.0 and up. (Older versions
would have required refraining from using string methods -- too
painful.)

Changed the -s option so that multiple -s options are cumulative.

Cheers,
mwh
 
A

Alex Martelli

Michael said:
a) I'm pretty sure timeit.py was written by Guido (he's not bad, either

Ooops yes -- it mentions Tim Peters in the docstring in a way that
should make it pretty obvious it's not written by Tim (only Julius
Caesar wrote of himself in 3rd person in just that way...)... silly me...!


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top