Loop vs Map vs List Comprehension - LeadSift - Steal Leads from Your Competitors
2158
post-template-default,single,single-post,postid-2158,single-format-standard,qode-quick-links-1.0,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,qode-theme-ver-11.2,qode-theme-bridge,wpb-js-composer js-comp-ver-5.2.1,vc_responsive

Loop vs Map vs List Comprehension

Posted on 17 Sep
on3-loops

As a Python developer I have seen lots of different preferences in this topic. These preferences aside, I was set to find which of them is faster in which situations.

TLDR;

If you require a list of results almost always use a list comprehension. If no results are required, using a simple loop is simpler to read and faster to run. Never use the builtin map, unless its more aesthetically appealing for that piece of code and your application does not need the speed improvement.

No Result Required

The first pattern I have seen is the use of Map, List Comprehension vs a standard loop in the case where there is no result required. Therefore the resulting list (returned by python’s map function or list comp) is ignored. For example, the following

for entry in entries:
    process(entry)

vs

map(process, entries)
[process(entry) for entry in entries]

The results are as follows, there were three variations of the test, the first is a single loop (O(n)), second is loop within a loop (O(n2)), and third is O(n3), three loops. The test code can be seen on github.

Result Required

This is the traditional pattern, where the generated list is useful. With this approach the following is how fast each approach is in python. For example

results = []
for entry in entries:
    results.append(process(entry))

vs

results = map(process, entries)
results = [process(entry) for entry in entries]

Same as the previous test, the three variations were tested, O(n), O(n2), and O(n3).

Conclusions

If you require a list of results almost always use a list comprehension. If no results are required, using a simple loop is simpler to read and faster to run. Never use the builtin map, unless its more aesthetically appealing for that piece of code and your application does not need the speed improvement.

1Comment
  • fallingleaver
    Posted at 03:54h, 08 March Reply

    Hi, I think you may want to make some modification to make the comparison fair.

    1. The list comprehension knows the array size at the beginning, while the for-loop code you wrote with “append()” cannot really do that. You may want to test it with preallocated list when the reusult is desired.

    2. The map slowness is because of the lambda closure construction. To make fair comparison, you would probably want to try (for n^3 case)

    x = [1,2,3,4]
    map(lambda a: f(*a), itertools.product(x,x,x))

    Not that your comparison is wrong, but it is premature to conclude something like “never use the builtin map because of its poor performance”. It might not be the map’s fault.

Post A Comment