Harpal
Harpal

Reputation: 12587

Python: For loop Vs. Map

I am currently in the process of optimising the translation part of my software, which translates co-ordinates x amount of times. My current translation code is in the translate function and the supposedly optimised portion in the translate_map function.

I read here that the map function should be used instead of for loops where possible because the loop is performed in C.

When I run a test case below, the map function actually runs slower than a standard for loop. Why does the map perform slower than the conventional for loop? How could I optimise the translate function to run faster?

import time

def translate(atom_list):
    for i in atom_list:
        i[1]+=1
        i[2]+=1
        i[3]+=1

atoms = [[1,1,1,1]]*1000
start = time.time()
for x in xrange(10000):
    translate(atoms)
print time.time() - start


atoms = [[1,1,1,1]]*1000
start = time.time()
def translate_map(atom_list):
    atom_list[1]+=1
    atom_list[2]+=1
    atom_list[3]+=1
for x in xrange(10000):
    map(translate_map,atoms)
print time.time() - start

output:

2.92705798149
4.14674210548

Upvotes: 1

Views: 5862

Answers (1)

Blckknght
Blckknght

Reputation: 104712

I suspect most of the overhead you're seeing with your map implementation comes from function call overhead. The translate function does all its work within a single loop, so there's just a single function call for the whole process. The implementation with map makes a separate function call for every item in the list.

A second source of overhead (though I suspect it is small compared to the function calls) is that map creates a list with the return values from the function. Since translate_map doesn't have a return statement, this will be all None values. Note that in Python 3, map is a generator, so your map version won't work at all unless you iterate over the results from the map call. The explicit loop is much clearer though, so I'd stick with that (if you don't go for numpy).

Oh, yes, numpy would make this much easier (and almost certainly faster too):

def translate(arr): # arr should be a numpy array
    arr += 1

That's it! No loops needed (at the Python level).

Upvotes: 3

Related Questions