优化循环。更快的ResultList.append([c,d,c [1] / d [1]])?阵列?地图?

时间:2014-02-06 14:30:35

标签: python

以下效果很好,但我想让它更快。实际的应用程序可以处理Tuple1和Tuple2,每个元素有30,000个元素和17个嵌套序列。我看到很多关于更快循环的问题,并尝试了一个没有改进的数组和映射。

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=[]

SomeFunction处理的ResultList的一个示例。

[(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]

1 个答案:

答案 0 :(得分:0)

只需从明确的for循环切换到列表理解并使用tuple代替list即可显着加快操作速度:

from itertools import product

ResultList = [(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

以下timeit试用版可以显示:

>>> 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
>>>