Help mi please with optimalization of simple Python code

Pedro RODRIGUEZ pedro_rodriguez at club-internet.fr
Tue Oct 1 13:20:33 EDT 2002


On Tue, 01 Oct 2002 17:43:13 +0200, Chrabros wrote:

> Hello,,
> I have written my first simple application in Python and I have a
> problem that processing of cca 10MB file takes ages (30 seconds). I
> think that problem is in data structure used. Could you please give me
> an advice how to optimize the following piece of code? Thank you in
> advance.
> 
>  Dalibor
> 
> 
> while y<maxY :
>   x=0
>   line = file.read(maxX*2)
>   while x<maxX :
>     val   = ord(line[x*2+2])*256 + ord(line[x*2+3])
>    ... here are some contditions which count the occurences
>        of unique  values of val
>     x=x+2
> y=y+2
> 
> I think that main problem is in usage of string "line" but I do not know
> how to it different way.
 
Could the standard 'struct' module help you to unpack a line faster ?

Something like :
   line_format = maxX * "h"
   ...
       vals = struct.unpack(line_fmt, line)

You may then perform your count on the resulting tuple.

Remark that you may need to adjust the format for endianess and since you
are skipping the two first chars.

Pedro



More information about the Python-list mailing list