Re: [Tutor] Increase speed

2007-10-09 Thread Michael Langford
You don't know what's slow. This is the perfect tool for a profiler.

http://docs.python.org/lib/profile.html

 --Michael

On 10/9/07, Øyvind <[EMAIL PROTECTED]> wrote:
>
> Hello.
>
> I have written a simple application that does a number of simple
> calculations. In psudo-code it looks something like below.
>
> The program works fine. However, it seems like I need a supercomputer to
> finish the resultwithin a reasonable timeframe, as the var.txt contains a
> lot of variables that has to be computed. And, since my budget doesn't
> include a supercomputer, I need to optimize the script. I have been able
> to half the running-time by flushing the file more seldom, and a little
> more by try and fail. However, half is still a looong time. I would like
> to increase the speed even more.
>
> 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?
>
> 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,1):
> n.verdier()
> for y in range(0,100):
> n.verdi()
>
> n.fil.write(result)
> if variable x > 0.32:
> break
>
>
>
> --
> This email has been scanned for viruses & spam by Domenebutikken -
> www.domenebutikken.no
> Denne e-posten er sjekket for virus & spam av Domenebutikken -
> www.domenebutikken.no
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Increase speed

2007-10-09 Thread Alan Gauld
"Øyvind" <[EMAIL PROTECTED]> wrote

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

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


> 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
directly.

>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
results.

> 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 
which.
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,1):
> 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.verdi()

n.fil.write(result)

n.fil was opened for reading not writing.

if variable x > 0.32:
break

It might be easier if you just posted the code.

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Increase speed

2007-10-09 Thread Eric Brunson
Øyvind wrote:
> Hello.
>
> I have written a simple application that does a number of simple
> calculations. In psudo-code it looks something like below.
>
> The program works fine. However, it seems like I need a supercomputer to
> finish the resultwithin a reasonable timeframe, as the var.txt contains a
> lot of variables that has to be computed. And, since my budget doesn't
> include a supercomputer, I need to optimize the script. I have been able
> to half the running-time by flushing the file more seldom, and a little
> more by try and fail. However, half is still a looong time. I would like
> to increase the speed even more.
>
> 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?
>   

I've had quite a bit of success using psyco (google it).  It only works 
on x86 platforms and I was unsuccessful getting it compiled on Solaris 
10 for x86, but I didn't try that hard, if you're on Linux you shouldn't 
have many problems.   I saw speedups anywhere from 2 to 10 times, 
depending on the code.

Hope that helps,e
e.

> 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,1):
> n.verdier()
> for y in range(0,100):
> n.verdi()
>
> n.fil.write(result)
> if variable x > 0.32:
> break
>
>
>
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Increase speed

2007-10-09 Thread Kent Johnson
Ø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,1):
> n.verdier()
> for y in range(0,100):
> n.verdi()
> 
> n.fil.write(result)
> if variable x > 0.32:
> break
> 
> 
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Increase speed

2007-10-09 Thread Øyvind
Hello.

I have written a simple application that does a number of simple
calculations. In psudo-code it looks something like below.

The program works fine. However, it seems like I need a supercomputer to
finish the resultwithin a reasonable timeframe, as the var.txt contains a
lot of variables that has to be computed. And, since my budget doesn't
include a supercomputer, I need to optimize the script. I have been able
to half the running-time by flushing the file more seldom, and a little
more by try and fail. However, half is still a looong time. I would like
to increase the speed even more.

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?

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,1):
n.verdier()
for y in range(0,100):
n.verdi()

n.fil.write(result)
if variable x > 0.32:
break



-- 
This email has been scanned for viruses & spam by Domenebutikken - 
www.domenebutikken.no
Denne e-posten er sjekket for virus & spam av Domenebutikken - 
www.domenebutikken.no

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor