Paul McGuire wrote: > "Jerry Hill" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Hello all, > > > > I have a piece of code I could use some help optimizing. What I'm > > attempting to do is periodically grab a screenshot, and search for 2D > > patterns of black pixels in it. I don't care about any color other > > than black. Here's some simple code that simulates my worst-case > > scenario, scanning the whole screen for a sequence that does not > > exist: > > > > import ImageGrab # From the PIL library > > > > def removeColor(rgb): > > r, g, b = rgb > > return (r == 0 and g == 0 and b == 0) > > > > BMP = ImageGrab.grab.getdata() > > x = map(removeColor, BMP) > > > > The idea is to search for sequences of black pixels on a background > > that can change colors. > > I had to do a similar thing using pywinauto to interact with a IE browser > running an embedded Flash application. To speed things up, I included psyco > to compile my functions. > > As far as working just in Python, you could remove the tuple unpacking > inside removeColor, and shorten it to just: > > def removeColor(rgb): > return rgb==(0,0,0) > > or even better: > > BLACK = (0,0,0) > def removeColor(rgb): > return rgb==BLACK > > By defining BLACK once and just referring to it by name, you also avoid > dynamically constructing the (0,0,0) tuple in every call.
Bzzzzzt. It's not dynamically constructed. Its a constant (in 2.4 at least) | >>> BLACK=(0,0,0) | >>> def func1(rgb): | ... return rgb==BLACK | ... | >>> def func2(rgb): | ... return rgb==(0,0,0) | ... | >>> import dis | >>> dis.dis(func1) | 2 0 LOAD_FAST 0 (rgb) | 3 LOAD_GLOBAL 1 (BLACK) | 6 COMPARE_OP 2 (==) | 9 RETURN_VALUE | >>> dis.dis(func2) | 2 0 LOAD_FAST 0 (rgb) | 3 LOAD_CONST 2 ((0, 0, 0)) | 6 COMPARE_OP 2 (==) | 9 RETURN_VALUE | >>> 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 -- http://mail.python.org/mailman/listinfo/python-list