[Tutor] Python performance on Windows system
Joel Ricker
joejava@dragoncat.net
Sat, 27 Oct 2001 00:56:54 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_0024_01C15E82.45BAE440
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi all,
I've been away from Python for a little while and so to stretch my =
programming legs a little bit, I've been working on a script to generate =
prime numbers. I've used a sieve algorithm and originally a list of =
numbers in an array and have reworked it to use a text file in the same =
way. I've noticed when working with big numbers, say finding all prime =
numbers between 1 and 5 million, my system becomes a little less =
responsive. It was real bad when I used a large array since sometimes =
the memory used by python would run into the 100 meg range but since =
I've switched to using a file it has improved alot but its still there.=20
Basically what happens is while the script is running, other windows =
are a little slow to appear and starting new programs or closing windows =
takes several seconds to hapen. I'm running Windows 2000, with a 750 =
mhz processor and 128megs of memory. What is odd though is that using =
Task Manager, I see that Python isn't using that much CPU -- only 1 or 2 =
percent at the most and very little memory -- about 520k. =20
Is it all the disk writes that is slowing things down? Anything I can =
do to my code to help things along? Or anything I can do to the python =
interpreter itself? Below is the main part of the code that is doing =
most of the work in the script. primein.txt is a text file containing a =
list of numbers between 0 and the max number to search to.
Thanks
Joel
f =3D open('primein.txt','r+') =20
x =3D 2
endsqrt =3D math.floor(math.sqrt(end))
while x <=3D endsqrt:
print x =20
for y in xrange(2, end/x+1):
f.seek(x * y * 12)
f.write('%10d\n' % 0)
f.flush()
f.seek(x * 12 + 12)
while 1:
x =3D f.readline()
if int(x) > 0:
x =3D int(x)
break =20
------=_NextPart_000_0024_01C15E82.45BAE440
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2479.6" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi all,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>I've been away from Python for a =
little while=20
and so to stretch my programming legs a little bit, I've been working on =
a=20
script to generate prime numbers. I've used a sieve algorithm and=20
originally a list of numbers in an array and have reworked it to use a =
text file=20
in the same way. I've noticed when working with big numbers, say finding =
all=20
prime numbers between 1 and 5 million, my system becomes a little less=20
responsive. It was real bad when I used a large array since =
sometimes the=20
memory used by python would run into the 100 meg range but since =
I've=20
switched to using a file it has improved alot but its still=20
there. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> Basically what happens is while =
the script is=20
running, other windows are a little slow to appear and starting new =
programs or=20
closing windows takes several seconds to hapen. I'm running =
Windows 2000,=20
with a 750 mhz processor and 128megs of memory. What is odd though =
is that=20
using Task Manager, I see that Python isn't using that much CPU -- only =
1 or 2=20
percent at the most and very little memory -- about 520k. =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Is it all the disk writes that is =
slowing things=20
down? Anything I can do to my code to help things along? Or =
anything=20
I can do to the python interpreter itself? Below is the main =
part of=20
the code that is doing most of the work in the script. primein.txt =
is a=20
text file containing a list of numbers between 0 and the max number to =
search=20
to.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Joel</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> f =3D=20
open('primein.txt','r+') <BR> x =3D=20
2<BR> endsqrt =3D=20
math.floor(math.sqrt(end))<BR> while x <=3D=20
endsqrt:<BR> print=20
x =20
<BR> for y in xrange(2,=20
end/x+1):<BR> =
=20
f.seek(x * y *=20
12)<BR> =
=20
f.write('%10d\n' % 0)<BR> =20
f.flush()<BR> f.seek(x * 12 +=20
12)<BR> while=20
1:<BR> =
x =3D=20
f.readline()<BR> &nb=
sp; =20
if int(x) >=20
0:<BR> &=
nbsp; =20
x =3D=20
int(x)<BR> &nb=
sp; =20
break </FONT></DIV></BODY></HTML>
------=_NextPart_000_0024_01C15E82.45BAE440--