[python-win32] range function problem

Chris Ingram Chris.Ingram@Synchrologic.com
Wed, 10 Oct 2001 09:54:15 -0400


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C15193.0C4DC430
Content-Type: text/plain;
	charset="gb2312"

Instead of using "range (2, 19999999)", try using "xrange (2, 19999999)".
The former actually builds a list in memory containing 19999998 elements
whereas the latter computes the value of each element on the fly, thus not
consuming much memory.
 

--
Chris Ingram, Synchrologic, Inc.
Email: Chris.Ingram@synchrologic.com
 

-----Original Message-----
From: zy [mailto:zhao_zi_long@etang.com]
Sent: Wednesday, October 10, 2001 9:06 AM
To: python-win32@python.org
Subject: [python-win32] range function problem


I am a new user of python language.
I tested a small program which likes the following:
********************************************
*  n = 19999999 
*  for x in range(2, n/2+1):
*       if n % x == 0:
*            print n, 'equals', x, '*', n/x
*            break
*  else:
*       print n, 'is a prime number'
**********************************************
I found something interesting when the variable 'n' equals 19999999 
that my computer seemed crazy because it began to eat my memory
at a surprising speed ,the memory amount used just rose from 131M to 258M 
and the process had no response. 
 
But when the variable 'n' equals 1999999, the program works well.
 
So I just ran the expression 'range(2, n/2+1)'(n=19999999),and i found
the same situation like the front happened.
 
So I want to know if it is a bug of the range function.
 
Thanks  first.


------_=_NextPart_001_01C15193.0C4DC430
Content-Type: text/html;
	charset="gb2312"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">


<META content="MSHTML 5.00.2919.6307" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=938135313-10102001>Instead of using "range (2, 19999999)", try using 
"xrange (2, 19999999)".&nbsp; The former actually builds a list in memory 
containing 19999998 elements whereas the latter computes the value of each 
element on the fly, thus not consuming&nbsp;much memory.</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<P><FONT size=2>--<BR>Chris Ingram, Synchrologic, Inc.<BR>Email: 
Chris.Ingram@synchrologic.com<BR></FONT>&nbsp;</P>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">
  <DIV align=left class=OutlookMessageHeader dir=ltr><FONT face=Tahoma 
  size=2>-----Original Message-----<BR><B>From:</B> zy 
  [mailto:zhao_zi_long@etang.com]<BR><B>Sent:</B> Wednesday, October 10, 2001 
  9:06 AM<BR><B>To:</B> python-win32@python.org<BR><B>Subject:</B> 
  [python-win32] range function problem<BR><BR></DIV></FONT>
  <DIV><FONT face="Courier New" size=2>I am a new user of python 
  language.</FONT></DIV>
  <DIV><FONT face="Courier New" size=2>I tested a small program which likes the 
  following:</FONT></DIV>
  <DIV><FONT face="Courier New" 
  size=2>********************************************<BR>*&nbsp;&nbsp;n = 
  19999999&nbsp;<BR>*&nbsp; for x in range(2, 
  n/2+1):<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if n % x == 
  0:<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  print n, 'equals', x, '*', 
  n/x<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  break<BR>*&nbsp;&nbsp;else:<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print n, 
  'is a prime number'</FONT></DIV>
  <DIV><FONT face="Courier New" 
  size=2>**********************************************</FONT></DIV>
  <DIV><FONT face="Courier New" size=2>I found something interesting when the 
  variable 'n'&nbsp;equals 19999999 </FONT></DIV>
  <DIV><FONT face="Courier New" size=2>that my computer </FONT><FONT 
  face="Courier New" size=2>seemed crazy because it began to eat my 
  memory</FONT></DIV>
  <DIV><FONT face="Courier New" size=2>at&nbsp;a surprising speed ,</FONT><FONT 
  face="Courier New" size=2>the&nbsp;memory amount used just rose from 131M to 
  258M </FONT></DIV>
  <DIV><FONT face="Courier New" size=2>and the process had no 
  response.&nbsp;</FONT></DIV>
  <DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
  <DIV><FONT face="Courier New" size=2>But when the variable 'n'&nbsp;equals 
  1999999,&nbsp;the program works well.</FONT></DIV>
  <DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
  <DIV><FONT face="Courier New" size=2>So I just ran the expression 'range(2, 
  n/2+1)'(n=19999999),and i found</FONT></DIV>
  <DIV><FONT face="Courier New" size=2>the same situation like the front 
  </FONT><FONT face="Courier New" size=2>happened.</FONT></DIV>
  <DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
  <DIV><FONT face="Courier New" size=2>So I want to know if it is a bug of the 
  range function.</FONT></DIV>
  <DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
  <DIV><FONT face="Courier New" size=2>Thanks 
&nbsp;first.</FONT></DIV></BLOCKQUOTE></BODY></HTML>

------_=_NextPart_001_01C15193.0C4DC430--