Element-wise Operation on Iterables

A simple example is to add two lists item-or-element-wisely, there are a few methods to achieve it, using map with operator.add or lambda, list comprehension or libraries like numpy.

There are pros and cons, some are easier to expand to operate on more iterables like summation on elements over three lists, others are easier to change the operations like add elements from first two and subtract element from the third.

The following is the benchmark of each method, ran with 3.4.5:
With 10,000 elements, average time of 1,000 runs
map with operator.add              : list(map(add, X, Y))          =   1,746 us
map with lambda                    : list(map(add, X, Y))          =   3,209 us
map with sum and zip               : list(map(sum, zip(X, Y)))     =   2,853 us
list comprehension with sum and zip: [sum(z) for z in zip(X, Y)]   =   3,901 us
list comprehension with + and zip  : [x + y for x, y in zip(X, Y)] =   1,665 us
numpy                              : X + Y                         =      24 us
Using numpy is the fastest way to produce the result, when you have lists each has ten thousands of items, then most likely numpy or some mathematics library is already in use and they have to be very efficient to perform operations.  In this case, the fastest non-numpy took 1.665 ms, that's 6,838% (nearly 7k%) slower than numpy's 24 us.

But if there is only a few elements, then it really doesn't matter.

Personally, I like the map with operator.add method, it's most clean if not consider using numpy.  The fastest one, list comprehension with + operator and zip is also a good option, it's very literal in expressing the operation.

Since this can work on iterables, therefore it doesn't have to be numeric types in lists, it can be anything iterable with operation to operate on the elements.

Comments

Popular posts from this blog

Changing decimal comma to point in logging asctime

Opening Note