Searching for patterns on the screen

Jerry Hill malaclypse2 at gmail.com
Fri Sep 15 11:39:22 EDT 2006


On 15 Sep 2006 08:16:41 -0700, John Machin <sjmachin at lexicon.net> wrote:
> C:\junk>python -mtimeit -s"BLACK=(0,0,0);rgb=(1,1,1)" "rgb==BLACK"
> 1000000 loops, best of 3: 0.129 usec per loop
>
> C:\junk>python -mtimeit -s"rgb=(1,1,1)" "rgb==(0,0,0)"
> 1000000 loops, best of 3: 0.127 usec per loop

Surprisingly (to me), hand splitting the tuple and doing short circuit
comparisons is slightly faster than either of those:

C:\Python25>python -mtimeit -s"BLACK=(0,0,0);rgb=(1,1,1)" "rgb==BLACK"
10000000 loops, best of 3: 0.18 usec per loop

C:\Python25>python -mtimeit -s"rgb = (1,1,1)" "rgb == (0,0,0)"
10000000 loops, best of 3: 0.169 usec per loop

C:\Python25>python -mtimeit -s"rgb = (1,1,1)" "rgb[0] == 0 and rgb[1]
== 0 and rgb[2] == 0"
10000000 loops, best of 3: 0.158 usec per loop

It probably has to do with not needing to find the len of the tuple
each time you compare.

I agree with Paul's earlier statement that there's probably a better
way to do this through PIL.  I've read through the PIL docs, but I'm
afraid I'm running into problems caused by my own ignorance. It seems
like I should be able to do something with Image.eval(), the point()
method or one of the PIL filters, but I don't understand them well
enough.  I'll take a look through the source, and if that doesn't help
I'll take this to the PIL list to get some further feedback on how
those work.

-- 
Jerry



More information about the Python-list mailing list