简体   繁体   English

为什么这个 python 语句卡住了?

[英]Why does this python statement got stuck?

Have anyone ever experienced this statement?有没有人经历过这种说法? The following code looks work well, but it makes my laptop get stuck when the exponent reaches 9 or above.下面的代码看起来运行良好,但当指数达到 9 或更高时,它会使我的笔记本电脑卡住。

ordered_tuple = tuple(range(10**9))

Every time I run this statement, my laptop slows down, got stuck taking up 100% of RAM usage.每次我运行这条语句时,我的笔记本电脑都会变慢,卡住占用 100% 的 RAM 使用率。 I searched for the reason why this happened, but there was no suitable answer.我搜索了发生这种情况的原因,但没有合适的答案。

Please help me understand why this code makes computers busy.请帮助我理解为什么这段代码会使计算机忙碌。 Thanks in advance.提前致谢。

I tried with different exponent between [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].我尝试使用 [1、2、3、4、5、6、7、8、9、10] 之间的不同指数。 1-8 works fairly well as I expected. 1-8 的效果和我预期的一样好。

ordered_tuple = tuple(range(10**1))
ordered_tuple = tuple(range(10**2))
ordered_tuple = tuple(range(10**3))
ordered_tuple = tuple(range(10**4))
ordered_tuple = tuple(range(10**5))
ordered_tuple = tuple(range(10**6))
ordered_tuple = tuple(range(10**7))
ordered_tuple = tuple(range(10**8))

whereas 9, 10 doesn't work.而 9、10 不起作用。

10**9 is 10 times as big as 10**8 . 10**9是 10 10**8的 10 倍。 10**10 is 100 times as big as 10**8 . 10**1010**8的 100 倍。 Likely your computer just does not have enough RAM to hold all these numbers.可能您的计算机没有足够的 RAM 来保存所有这些数字。

PS: Not sure how long is a Python int, if it is 4-byte long, then 10**9 of them is 4 GB, but then 10**10 does not fit in 4-byte long int and needs more. PS:不确定Python int有多长,如果是4字节长,那么其中的10**9就是4GB,但是10**10不适合4字节的long int,需要更多。 If it is 8-byte long, then 10**9 is 8 GB and 10**10 is 80 GB.如果是8字节长,那么10**9就是8GB, 10**10就是80GB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM