Speedup Bitmap Parser

Peter Otten __peter__ at web.de
Tue Feb 17 18:26:07 EST 2009


Jayson Santos wrote:

> On 17 fev, 14:00, bearophileH... at lycos.com wrote:
>> Jayson Santos:
>>
>> > After changing my code to use only functions instead classes
>> > my code is too much faster.
>> > Here cProfile statistcs:
>> > Using old code : 633608 function calls in 1.361 CPU seconds
>> > Using new code: 475487 function calls in 0.800 CPU seconds
>>
>> If you show a pastebin of the new version (you can also paste it, for
>> reference for future people, when the pastebin will be deleted),
>> probably there are ways to improve it more.
>> And I also suggest you to try Psyco. (Someone recently has shown here
>> a compiled version for Windows of Psyco for Python 2.6).
>>
>> Bye,
>> bearophile
> 
> I'm using linux with python 2.5
> Here is the result using psyco.full(): 93 function calls in 0.243 CPU
> seconds
> And here is the final code: http://pastebin.com/f3e20d669

I think you are heading in the wrong direction. Turning classes into
dictionaries in code that runs just once (like the Bitmap class) makes your
program harder to read, not faster. Adding globals makes it non-reentrant.
You should really concentrate your optimization efforts on the inner loops.
Here avoiding loops written in Python and using tuples instead of a custom
class may offer significant speedups:

def bitmap_line(s):
    a = array.array("B")
    a.fromstring(s)
    return zip(*(iter(a),)*3)
 
Note that I didn't test or time it because your script requires other
changes to run on AMD64.

Peter



More information about the Python-list mailing list