Nested loops is strangely slow, totally at a loss.

Shiyao Ma i at introo.me
Wed Dec 10 00:20:25 EST 2014


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



More information about the Python-list mailing list