[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re: nested loops comparion



I have run these tests about 10-15 times now, and am consistently getting
the same results.

Why is example B so slow? One would think it is doing exactly the same thing
as Example A?
This may be coming from the fact that Python caches 'low integer' objects. Therefor Python is just increasing a refcount and returning it's cached 0 object. For the larger values a python object is being allocated and then freed.

From my experiences, if you are going to access items in a Numeric array with nested loops, you are actually better just using regular pygame nested lists. The point of Numeric is that you can work with the numbers
in the array without accessing each element inside nested loops.

These both of course would pale in comparison to this example, but I am guessing you have a more complex block of code inside your deep nest.
d = map.sample(100,100)
s = time.time()
for i in xrange(10000):
d + 1
print time.time() - s


#Example A
d = Numeric.zeros((10,10))
s = time.time()
for i in xrange(10000):
    for y in xrange(len(d)):
        for x in xrange(len(d[y])):
            t = d[y,x]
            t + 1
print time.time() - s

1.50099992752

#Example B
#map.sample returns a 10x10 array take from a surfarray.array2d()
d = map.sample(100,100) s = time.time()
for i in xrange(10000):
for y in xrange(len(d)):
for x in xrange(len(d[y])):
t = d[y,x]
t + 1
print time.time() - s

10.5379998684