Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82261

Nested loops is strangely slow, totally at a loss.

Date 2014-12-10 13:20 +0800
From Shiyao Ma <i@introo.me>
Subject Nested loops is strangely slow, totally at a loss.
Newsgroups comp.lang.python
Message-ID <mailman.16794.1418188833.18130.python-list@python.org> (permalink)

Show all headers | View raw


When doing nested loop, the very first iteration of the innermost loop ends ultimately slow.

Let's the code speak.

The following code is quite contrived. Actually it's derived from my 3d-dct script.
The actual difference is way more significant than this example.

In case of any evil of gmail, bpaste here: https://bpaste.net/show/edfef62edb17

# this constructs a space_len x space_len x space_len 3D coordinate
import timeit
from itertools import product
space_len = 580
space = product(xrange(space_len), xrange(space_len), xrange(space_len))


sparse_cloud = product(xrange(1), xrange(1), xrange(1))

for i, j, k in sparse_cloud:
    ts = timeit.default_timer()
    if (i, j, k) in space: pass
    te = timeit.default_timer()
    print("A, finish a loop with ", te-ts)
print("Done Test A")


sparse_cloud = product(xrange(1000), xrange(1000), xrange(1000))
for i, j, k in sparse_cloud:
    ts = timeit.default_timer()
    if (i, j, k) in space: pass
    te = timeit.default_timer()
    print("B, finish a loop with ", te-ts)
print("Done Test B")


# example output
"""
('A, finish a loop with ', 2.1457672119140625e-06)
Done Test A
('B, finish a loop with ', 8.736134052276611)
('B, finish a loop with ', 1.9073486328125e-06)
('B, finish a loop with ', 0.0)
('B, finish a loop with ', 0.0)
('B, finish a loop with ', 1.1920928955078125e-06)
('B, finish a loop with ', 9.5367431640625e-07)
('B, finish a loop with ', 9.5367431640625e-07)
...
"""

We can see that the first iteration of B ends rather slow, 8.7 seconds here.

Why? I am curious about the internals, what's happening under the hood that makes this happen?

Thanks in advance!


-- 
Shiyao Ma
http://introo.me

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Nested loops is strangely slow, totally at a loss. Shiyao Ma <i@introo.me> - 2014-12-10 13:20 +0800
  Re: Nested loops is strangely slow, totally at a loss. Steven D'Aprano <steve@pearwood.info> - 2014-12-10 06:44 +0000
    Re: Nested loops is strangely slow, totally at a loss. Chris Angelico <rosuav@gmail.com> - 2014-12-10 17:53 +1100
      Re: Nested loops is strangely slow, totally at a loss. Steven D'Aprano <steve@pearwood.info> - 2014-12-10 07:21 +0000
    Re: Nested loops is strangely slow, totally at a loss. Terry Reedy <tjreedy@udel.edu> - 2014-12-10 02:16 -0500

csiph-web