Speedup Bitmap Parser

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Feb 16 16:29:11 EST 2009


Jayson Santos:
> Hello people, I'm writing a Bitmap parser for study purposes,

This code:

        x = 0
        y = 0
        for line in bmp.bitmap_lines.lines:
            for color in xrange(len(line.colors)):
                canvas.create_line(x, y, x+1, y, fill="#%02x%02x%02x"
% (line.colors[color].red, line.colors[color].green, line.colors
[color].blue))
                x += 1
            x = 0
            y += 1


May be replaced with something like:

for x, color in enumerate(line.colors):
    canvas.create_line(x, y, x+1, y, fill="#%02x%02x%02x" %
(color.red, color.green, color.blue))

And maybe a RGBTriple may be replaced with an immutable namedtuple.
Using nested classes may be overkill for this program.

Use the Python profiler and profile your code with a real image, and
show us/me the profiler results, so I can give you some suggestions to
speed up your code.

Bye,
bearophile



More information about the Python-list mailing list