[Tutor] Increase speed

Kent Johnson kent37 at tds.net
Tue Oct 9 12:49:15 CEST 2007


Øyvind wrote:
> Hello.
> 
> I have written a simple application that does a number of simple
> calculations. In psudo-code it looks something like below.

I think this is the first time I have ever been asked to optimize code 
without looking at the code! It's hard to optimize pseudo-code. Can you 
show the real code? I will make some general suggestions.

> Does anyone have any suggestions of what I should do? Is Stackless Python
> an option? Is there some other steps I could take? Some basic steps?

AFAIK Stackless is not faster for single-threaded calculations.

Unfortunately heavy-duty number crunching is not a strong point of 
Python (because every operation requires a virtual method call). Try 
psyco. Use numpy if you can. Move the calculations into C code (maybe 
with pyrex). Move loop control into C code by using list comprehension 
or map. Reduce the number of name lookups. Make names local.

Kent

> 
> Thanks in advance,
> Øyvind
> 
> class start:
> 
>     filles = open("var.txt","r") into memory
> 
>     def oppned(self):
>         return randint(0,1)
> 
>     def verdier(self):
>         increase variable x and y
> 
>     def verdi(self):
>         for i in filles:
>             generate random from oppned
>             simple calculation
> 
>             if result 1:
>                  write result
> 
>             if result 2:
>                  generate new random, calculate more
>                  write result
> 
>             if result 3:
>                  write result, use new variable
> 
> if __name__ == '__main__':
>     n = start()
>     for x in range(0,10000):
>         n.verdier()
>         for y in range(0,100):
>             n.verdi()
> 
>         n.fil.write(result)
>         if variable x > 0.32:
>             break
> 
> 
> 



More information about the Tutor mailing list