Python For 循环随时间变慢
所以我在处理 Python 中的 for 循环时遇到了一些麻烦 - 据我所知,它们随着时间的推移变得越来越慢。我在范围内的范围内循环,随着时间的推移,循环明显变慢。如果重要的话,这是在游戏引擎内部完成的。谁能告诉我问题是什么?
这是一个简单的例子。
for x in range(xs): # xs, ys, and zs are all pre-determined size values
for z in range(zs):
for y in range(ys):
vp = [x * vs, y * vs, z * vs]
v = Cube(vp)
这个过程的初始速度还不错,但随着时间的推移,循环速度会减慢。我知道它与游戏引擎的光栅化器不同,因为当循环完成时,引擎的其余部分以 60 FPS 运行。那么可能是什么问题呢?
编辑:我使用的是Python 3,所以没有xrange。
编辑2:对于此示例,vs 为 1.0,xs、ys 和 zs 的预定大小值均为 20。
So I'm having a little trouble dealing with for loops in Python - as far as I can tell, they're getting slower with time. I'm looping over a range inside of a range, and as time passes, the loop noticeably slows. This is done inside of a game engine, if it matters. Could anyone tell me what the issue is?
Here's a quick example.
for x in range(xs): # xs, ys, and zs are all pre-determined size values
for z in range(zs):
for y in range(ys):
vp = [x * vs, y * vs, z * vs]
v = Cube(vp)
The initial speed of this process is fine, but with time the loop slows. I know it's not anything else like the Rasterizer of the game engine because when the loop is done, the rest of the engine runs at 60 FPS. So what could be the problem?
EDIT: I'm using Python 3, so there is no xrange.
EDIT 2: For this example, vs is 1.0, and the predetermined size values of xs, ys, and zs are all 20.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这又是一个“需要更多信息”的案例。然而,Python 有一种有效构建嵌套循环的标准方法,
itertools .product
:不需要在内循环中每次都构造
range
。我还将您对range
的使用切换为xrange
,因为它更适合大范围,尽管这实际上与product
无关。@JohnZ 的问题很好——如果你的“预定大小值”非常大,特别是如果
vs
也很大,你可能会构造一些大的值,并且可能需要很长时间Cube
来处理它们。我怀疑循环本身正在减慢,但数字正在变大,所以你的计算可能会变慢。
This is another case of "need more information". However, Python has a standard way of constructing nested loops like this efficiently,
itertools.product
:It doesn't require the construction of
range
s every time in the inner loop. I also switched your use ofrange
toxrange
, as it's better for large ranges, although this is really irrelevant withproduct
.@JohnZ's question is good -- if your "predetermined size values" are very large, and especially if
vs
is also large, you could be constructing some large values, and it could be taking a long time forCube
to process them.I doubt the loop itself is slowing down, but the numbers are getting larger, so your calculations might be.
我能想到的三件事:
内存 - 如果您将所有生成的值存储在某处,则循环可能会减慢,因为所有内存都被使用。 Python 有自己的内存管理器,因此使用大量内存最终可能会使程序变慢。
计算的复杂性 - Python 使用任意精度的数值数据类型。如果您将极大的数字相乘(尤其是浮点数),程序将会变慢。这实际上取决于这些值有多大。
Cube - 这可能是 Cube 代码中的一个错误(尽管我确信它可能就像听起来那么简单)。
Three things I can think of:
Memory - if you're storing all of the generated values somewhere, the loop might be slowing down because the of all the memory being used. Python has it's own memory manager, so using up lots of memory could eventually make the program slower.
Complexity of calculations - Python uses arbitrary precision numeric data types. If you are multiplying extremely large numbers together (especially floats) the program will slow down. It really depends on how large these values get.
Cube - It could be a bug in the Cube code (although I'm sure it's probably just as simple as it sounds).