[Tutor] Increase speed

Alan Gauld alan.gauld at btinternet.com
Tue Oct 9 16:36:06 CEST 2007

"Øyvind" <python at kapitalisten.no> wrote

> Does anyone have any suggestions of what I should do?

Your pseudo code is a bit too pseudo to be useful for performance

> class start:

You don;t really need a class here, somple functions will
suffice but won't make much difference to speed.

>    filles = open("var.txt","r") into memory

Not sure what you mean by "into memory" here.
Are you storing the open file handle or the contents
of the file?

>    def oppned(self):
>        return randint(0,1)

Since you only return randint you can do away with
the function and its call overhead and just call randint

>    def verdier(self):
>         increase variable x and y

You don't specify how these are increased nor where
they come from. If you mean the loop indices below
then directly modifying loop items inside the loop
can be a risky move leading to difficult to predict

>     def verdi(self):
>         for i in filles:

This will either be a sequence of characers if you read the contents
or a series of lines if you just stored the file object. Not sure 
Also, not knowing the format or content of the file makes it hard to
guess what is going on!

>             generate random from oppned

just use randint()

>             simple calculation

>            if result 1:
>                 write result

Where are you writing it to? stdout or to a file?
If to a file are you open/closing it each write or do you open it
once and only close at the end? Hopefu;lly its not the same file
you are reading from, or things could get confused!

>            if result 2:
>                 generate new random, calculate more
>                 write result
>            if result 3:
>                 write result, use new variable

As above.

> if __name__ == '__main__':
>     n = start()

Not needed if you do away with the class.

>     for x in range(0,10000):
>         n.verdier()

This was supposed to change y. However y gets reset
immediately by the loop. so it appears the y processing
is wasted.

        for y in range(0,100):


n.fil was opened for reading not writing.

        if variable x > 0.32:

It might be easier if you just posted the code.

Alan G. 

More information about the Tutor mailing list