Python For 循环随时间变慢

发布于 2024-11-30 09:34:07 字数 550 浏览 1 评论 0原文

所以我在处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2024-12-07 09:34:07

这又是一个“需要更多信息”的案例。然而,Python 有一种有效构建嵌套循环的标准方法,itertools .product

from itertools import product

for x, y, z in product(xrange(xs), xrange(zs), xrange(ys)):
    vp = [x * vs, y * vs, z * vs]
    v = Cube(vp)

不需要在内循环中每次都构造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:

from itertools import product

for x, y, z in product(xrange(xs), xrange(zs), xrange(ys)):
    vp = [x * vs, y * vs, z * vs]
    v = Cube(vp)

It doesn't require the construction of ranges every time in the inner loop. I also switched your use of range to xrange, as it's better for large ranges, although this is really irrelevant with product.

@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 for Cube to process them.

I doubt the loop itself is slowing down, but the numbers are getting larger, so your calculations might be.

缱绻入梦 2024-12-07 09:34:07

我能想到的三件事:

内存 - 如果您将所有生成的值存储在某处,则循环可能会减慢,因为所有内存都被使用。 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文