我正在尝试优化对象排列的迭代。 对于每对两个对象,执行操作。结果存储在字典中两次:对于x,y密钥对和y,x密钥对,以便于访问结果。 这是我的代码:
import timeit
from itertools import permutations
MAX = 100
l2 = range(0, MAX)
l2 = [i for i in permutations(l2, 2)]
l1 = range(0, MAX)
# do the permutations inside two for loops
def f1():
d = {}
for x, y in l2:
if x not in d:
d[x] = {}
if y not in d:
d[y] = {}
for x in l1:
for y in l1:
if x == y:
# result is not needed
continue
else:
if y not in d[x]:
res = x*y
d[x][y] = res
d[y][x] = res
# use the tuples generated by permutations of itertools
def f2():
d = {}
for x, y in l2:
if x not in d:
d[x] = {}
if y not in d:
d[y] = {}
for x, y in l2:
#if x == y:
# result is not needed
# continue
#else:
if y not in d[x]:
res = x*y
d[x][y] = res
d[y][x] = res
if __name__ == '__main__':
res1 = min(timeit.repeat("f1()", "from __main__ import f1",
number=1000))
res2 = min(timeit.repeat("f2()", "from __main__ import f2",
number=1000))
print res1, res2, res1/res2
# results in e.g. 2.65521230858 3.10015283881 0.856477872748
# result after update: 3.52828623172 3.2469033563 1.08666191893
我试图理解为什么 f1()仅占用 f2()的约85%的时间。 根据我的理解, f2()应该更快,因为它不需要迭代第二个列表。
有人可以解释为什么这不是真的吗?
UPDATE :我刚刚看到f2需要x == y条件,因为x在这里永远不等于y。还移动了for循环之外的第一级字典的创建,以避免来自它的影响。现在f2的速度提升了~8%。