Easy function, please help.

rantingrick rantingrick at gmail.com
Thu Feb 10 14:47:24 EST 2011


On Feb 10, 11:01 am, Terry Reedy <tjre... at udel.edu> wrote:
> On 2/10/2011 11:52 AM, Ethan Furman wrote:
>
> > Jason Swails wrote:
> >> How is "while n != 0:" any worse?
>
> 1. It is redundant, just like 'if bool_value is not False:'.
> Python programmers should understand the null value idiom.
>
> 2. It does 2 comparisons, 1 unneeded, instead of 1. For CPython,
> it adds 2 unnecessary bytecode instructions and takes longer.
>
>  >>> from dis import dis
>  >>> def f(n):
>         while n: pass
>
>  >>> dis(f)
>    2           0 SETUP_LOOP              10 (to 13)
>          >>    3 LOAD_FAST                0 (n)
>                6 POP_JUMP_IF_FALSE       12
>                9 JUMP_ABSOLUTE            3
>          >>   12 POP_BLOCK
>          >>   13 LOAD_CONST               0 (None)
>               16 RETURN_VALUE
>  >>> def f(n):
>         while n != 0: pass
>
>  >>> dis(f)
>    2           0 SETUP_LOOP              16 (to 19)
>          >>    3 LOAD_FAST                0 (n)
>                6 LOAD_CONST               1 (0)
>                9 COMPARE_OP               3 (!=)
>               12 POP_JUMP_IF_FALSE       18
>               15 JUMP_ABSOLUTE            3
>          >>   18 POP_BLOCK
>          >>   19 LOAD_CONST               0 (None)
>               22 RETURN_VALUE
>
> >> It has exactly the same effect without adding any code
>
> Untrue, see above.

Overblown. See below!

Terry i apologize for saying this but you offer a *really* weak
argument here. The load constant should only happen once. The
COMPARE_OP could be called "an extra step" but such a little problem
can easily be optimized away on the CPython side of things. It is
obvious that POP_JUMP_IF_FALSE is optimized for boolean comparisons.
And even if you throw out my fine solution...

1. When has 2 bytecode instructions EVER out weighed a solid
readability feature's incorporation into Python?

2. How many zillions of iterations would it take to notice even a
*microscopic* speed reduction?

Remember, Python IS "first and foremost" the language of readability.
Why do you think indention is enforced? Why do think the number of
built-in methods is kept small? Why do think magic methods are wrapped
in leading and trailing double underscores? Why do you think the
keyword set is simplistic and small? Why? Why? Why? i could go on all
day...  Terry, *we* are the shining jewel of coherency in a dark sea
polluted with multiplicity which is violently agitated by syntactical
asininity.



More information about the Python-list mailing list