The following works well but I'd like to make it faster. The actual application could process Tuple1 and Tuple2 each with 30,000 elements and 17 nested sequences per element. I see numerous questions about faster loops and have tried an array and map without improvement.
for i in Tuple1:print i((1, 2.2, 3), (2, 3.3, 4))
((5, 6.6, 7), (6, 7.7, 8))for i in Tuple2:print i((10, 11, 12), (11, 12, 13), (12, 13, 14))
((20, 21, 22), (21, 22, 23), (22, 23, 24))ResultList = []for a in Tuple1:for b in Tuple2:for c in a:for d in b:ResultList.append( [c, d, c[1]/d[1]] )SomeFunction() # Processes ResultList and is not a concern.ResultList=[]
An example of a ResultList processed by SomeFunction.
[(1, 2.2, 3), (10, 11, 12), 0.2]
[(1, 2.2, 3), (11, 12, 13), 0.18333333333333335]
[(1, 2.2, 3), (12, 13, 14), 0.16923076923076924]
[(2, 3.3, 4), (10, 11, 12), 0.3]
[(2, 3.3, 4), (11, 12, 13), 0.27499999999999997]
[(2, 3.3, 4), (12, 13, 14), 0.25384615384615383]
Simply switching from an explicit for
loop to a list comprehension and using a tuple
instead of a list
can significantly speed up your operation:
from itertools import productResultList = [(c, d, c[1] / d[1]) for a, b in product(t1, t2) for c, d in product(a, b)]
# ^Use a tuple here instead of a list
As can be shown by the timeit
trial below:
>>> from timeit import Timer
>>> original_test = Timer('original_version(Tuple1, Tuple2)', '''\
... Tuple1 = (
... ((1, 2.2, 3), (2, 3.3, 4)),
... ((5, 6.6, 7), (6, 7.7, 8))
... )
... Tuple2 = (
... ((10, 11, 12), (11, 12, 13), (12, 13, 14)),
... ((20, 21, 22), (21, 22, 23), (22, 23, 24))
... )
... def original_version(t1, t2):
... ResultList = []
... for a in t1:
... for b in t2:
... for c in a:
... for d in b:
... ResultList.append([c, d, c[1] / d[1]])''')
>>> improved_test = Timer('improved_version(Tuple1, Tuple2)', '''\
... from itertools import product
... Tuple1 = (
... ((1, 2.2, 3), (2, 3.3, 4)),
... ((5, 6.6, 7), (6, 7.7, 8))
... )
... Tuple2 = (
... ((10, 11, 12), (11, 12, 13), (12, 13, 14)),
... ((20, 21, 22), (21, 22, 23), (22, 23, 24))
... )
... def improved_version(t1, t2):
... return [(c, d, c[1] / d[1]) for a, b in product(t1, t2) for c, d in product(a, b)]''')
>>> original_time = original_test.timeit()
>>> improved_time = improved_test.timeit()
>>> print 'Improved version is %{} faster'.format(
... (original_time - improved_time) / original_time * 100
... )
Improved version is %30.0181954314 faster
>>>