Quantcast

MemoryError?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

MemoryError?

Litvin-2
Can someone please explain why

n = 50000000
lst = [i for i in range(n)] # or xrange(n) in Python 2

crashes with MemoryError on a 32-bit system, while

n = 50000000
lst = n*[0]
for i in range(n):  # or xrange(n) in Python 2
    lst[i] = i

works?

Thanks,

Gary Litvin
www.skylit.com


_______________________________________________
Edu-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/edu-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MemoryError?

David MacQuigg
No problem with Python 2.7.3 on a MacBookPro with 2GB memory and a light load of other programs.  Could it be dependent on how much memory you have available?  

There might be slight differences in the memory consumption in computing the two different forms.  I would guess the second form might take less memory because it allocates exactly what it needs, not some larger amount based on a dynamic allocation algorithm for expanding lists.



From: Litvin <[hidden email]>
To: [hidden email]
Sent: Saturday, April 21, 2012 1:50 PM
Subject: [Edu-sig] MemoryError?

Can someone please explain why

n = 50000000
lst = [i for i in range(n)] # or xrange(n) in Python 2

crashes with MemoryError on a 32-bit system, while

n = 50000000
lst = n*[0]
for i in range(n):  # or xrange(n) in Python 2
    lst[i] = i

works?

Thanks,

Gary Litvin
www.skylit.com


_______________________________________________
Edu-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/edu-sig



_______________________________________________
Edu-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/edu-sig
Loading...