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:
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
X = range(4) | |
Y = range(4) | |
from operator import add | |
OP_ADD = list(map(add, X, Y)) | |
LAMBDA = list(map(lambda x, y: x + y, X, Y)) | |
SUMZIP = list(map(sum, zip(X, Y))) | |
COMSUM = [sum(z) for z in zip(X, Y)] | |
COMZIP = [x + y for x, y in zip(X, Y)] | |
import numpy as np | |
AX = np.arange(4) | |
AY = np.arange(4) | |
NP_ADD = AX + AY |
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 usUsing 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
Post a Comment