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

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Jul 11 20:02:20 EDT 2010


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.



-- 
Steven



More information about the Python-list mailing list