pointless musings on performance
Rob Williscroft
rtw at freenet.co.uk
Tue Nov 24 09:44:24 EST 2009
mk wrote in news:mailman.923.1259070092.2873.python-list at python.org in
comp.lang.python:
> MRAB wrote:
>> In what way is it counterintuitive? In 'pythonic' the conditions are
>> simpler, less work is being done, therefore it's faster.
>
> But the pythonic condition is more general: nonevar or zerovar can be
> '', 0, or None. So I thought it was more work for interpreter to compare
> those, while I thought that "is not None" is translated to one, more
> low-level and faster action. Apparently not.
>
> As Rob pointed out (thanks):
>
> 11 31 LOAD_FAST 0 (nonevar)
> 34 JUMP_IF_FALSE 4 (to 41)
>
> I'm no good at py compiler or implementation internals and so I have no
> idea what bytecode "JUMP_IF_FALSE" is actually doing.
IIUC it implements:
http://docs.python.org/3.1/reference/expressions.html#boolean-operations
"In the context of Boolean operations, and also when expressions are used
by control flow statements, the following values are interpreted as false:
False, None, numeric zero of all types, and empty strings and containers
(including strings, tuples, lists, dictionaries, sets and frozensets). All
other values are interpreted as true. User-defined objects can customize
their truth value by providing a __bool__() method."
In particular its implementing "... Boolean operation ... used by
control flow ...", all in one handy op code.
Rob.
More information about the Python-list
mailing list