integer >= 1 == True and integer.0 == False is bad, bad, bad!!!

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Jul 11 20:18:52 EDT 2010


On 12/07/2010 01:02, Steven D'Aprano wrote:
> On Sat, 10 Jul 2010 23:50:05 -0700, rantingrick wrote:
>
>> You do realize that
>> Python must build a tuple for ever conditional that uses this semantic?
>> This is more bad, bad, bad than integer bool-ing! My bin packer could
>> potentially compute millions of parts. I do not want to waste valuable
>> processor cycles building numerous tuples just for the sake of a
>> conditional "condition"! This is bad coding style Stephen.
>
> No, premature optimization is bad coding.
>
> Building a tuple is extremely fast:
>
> $ python -m timeit "x = ()"
> 1000000 loops, best of 3: 0.316 usec per loop
> $ python -m timeit "x = False"
> 1000000 loops, best of 3: 0.36 usec per loop
>
>
> Testing is fast too:
>
> $ python -m timeit "x = (); 1 if x else 2"
> 1000000 loops, best of 3: 0.663 usec per loop
> $ python -m timeit "x = False; 1 if x else 2"
> 1000000 loops, best of 3: 0.969 usec per loop
>
>
> You've been around here long enough that you should know better. Stop
> wasting your time, and ours, ranting over the enormous cost of things
> that aren't costly at all. Come back when you have profiled your code and
> can prove that the cost of building empty tuples is an actual bottleneck.
>

+1

Kindest regards.

Mark Lawrence





More information about the Python-list mailing list