Optimizing Loop. Faster Resultlist.append( [ C, D, C[1]/d[1]] )? Array? Map?
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 s
Solution 1:
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 product
ResultList =[(c, d,c[1]/ d[1])for a, b in product(t1, t2)forc, 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
>>>
Post a Comment for "Optimizing Loop. Faster Resultlist.append( [ C, D, C[1]/d[1]] )? Array? Map?"