Help mi please with optimalization of simple Python code
sismex01 at hebmex.com
sismex01 at hebmex.com
Tue Oct 1 12:24:22 EDT 2002
> From: Chrabros [mailto:chrabros at seznam.cz]
>
> 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.
>
How about this? (I assume there's a "y=0" before the "while y..:"
----------------------------------------------------------
# Precalculate the inner-loop's index range.
XRANGE = [ (2*x,2*x+1) for x in range(0, maxX) ]
# I assume there's a missing "y=0" somewhere.
for y in range(0, maxY, 2):
# Obtain a "line", convert to a list of integers.
line = map(ord, list(file.read(maxX*2)))
# Iterate over our pre-calculated indexes.
for a,b in XRANGE:
val = line[a]*256 + line[b]
... here are some contditions which count the occurences
of unique values of val
----------------------------------------------------------
OK, I don't know if you're using python 2.2, if you're not,
then you can use the list comprehension at the beginning.
Why a list comprehension AND a map()? Because ord() is
implemented in C, and map() also, this gives a maximum
boost.
Good luck, hope this helps.
-gustavo
pd: can you identify your bug, and mine?
Advertencia:
La informacion contenida en este mensaje es confidencial y restringida y
esta destinada unicamente para el uso de la persona arriba indicada, Esta
comunicacion representa la opinion personal del remitente y no refleja
necesariamente la opinion de la Compañia. Se le notifica que esta
estrictamente prohibida cualquier difusion, distribucion o copia de este
mensaje. Si ha recibido esta comunicacion o copia de este mensaje por error,
o si hay problemas en la transmision, favor de comunicarse con el remitente.
Todo el correo electrónico enviado para o desde esta dirección será
procesado por el sistema de correo corporativo de HEB. Tal correo
electrónico esta sujeto a ser almacenado y puede ser revisado por alguien
ajeno al recipiente autorizado con el propósito de monitorear que se cumplan
las normas de seguridad de la empresa.
More information about the Python-list
mailing list