[英]Improve speed performance of a python code
function transform_input
执行以下操作:
combination
参数是包含 1 或 2 个值的元组列表(此列表的大小通常最大为 6)。combination
内的所有冗余。 我所说的冗余是删除重复项并删除包含交换的相同值的元组。 冗余元组的示例: (2,3) and (3,2)
、 (7,) and (7,)
、 (1,2) and (1,2)
。(2,4)
而不是(4,2)
。我想出的代码是下面的代码(带有一个运行示例):
def transform_input(combination):
combination = [list(elem) for elem in combination]
for t in combination:
t.sort()
combination = [tuple(elem) for elem in combination]
combination = list(set(combination))
return combination
my_input = [
[(3,8), (8,3), (7,)],
[(8,8), (8,8)],
[(3,), (7,), (6,), (3,), (7,)],
[(2,3), (3,2)],
[(2,), (2,)]
]
for comb in my_input:
print(transform_input(comb))
output 是:
[(3, 8), (7,)]
[(8, 8)]
[(6,), (7,), (3,)]
[(2, 3)]
[(2,)]
有没有办法可以提高transform_input
的速度性能?
这段代码在我的真实程序中执行了很多,每一次改进都会很棒。
这里还有另外两种可能的方法; 第一个是Błotosmętek发布的答案的轻微变化,使用{}
而不是set
(在元素很少的列表上应该更快,看看这里和这里)。
def transform_input3(combination):
return list({tuple(sorted(elem)) for elem in combination})
另一种方法基于map
function(即,使用迭代器)。
def transform_input4(combination):
return list(set(map(tuple, map(sorted, combination))))
如果内部元组不能包含超过 2 个元素,您可以实现类似这样的操作,以避免对它们中的每一个进行排序以及从list
转换为tuple
:
def transform_input6(combination):
return [t for t in {x[::-1] if x[0] > x[-1] else x for x in combination}]
我在您的my_input
中添加了一些项目(以及alireza yazdandoost使用的项目)并对所有方法进行了基准测试,在每个项目上运行每个 function 100 次; 这是新的my_input
:
my_input = [
[(3, 8), (8, 3), (7,)],
[(3, 8), (8, 3), (7,), (3, 8), (8, 3), (7,)],
[(8, 8), (8, 8)],
[(3,), (7,), (6,)],
[(3,), (7,), (6,), (3,), (7,), (3,)],
[(2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2)],
[(2,), (2,)],
[(2,), (2,), (2,), (2,), (2,), (2,)],
[(3, 8), (8, 3), (7,), (2, 3), (3, 2), (2, 3)],
[(3, 8), (8, 3), (7,), (3, 4), (8, 8), (8, 8), (3,), (7,), (6,), (3,), (7,), (2, 3), (3, 2), (2,), (2,)],
]
这些是基准测试的结果( my_transform
是您的原始代码, my_transform2
是 Błotosmętek 的答案, my_transform5
是 alireza yazdandoost 的答案),作为执行时间的平均值和标准偏差(以秒为单位):
[(3, 8), (8, 3), (7,)]
transform_input: 2.524e-06 (9.738e-07)
transform_input2: 2.067e-06 (2.728e-07)
transform_input3: 1.750e-06 (1.570e-07)
transform_input4: 1.804e-06 (1.984e-07)
transform_input5: 1.116e-06 (2.566e-07)
transform_input6: 1.051e-06 (1.874e-07)
[(3, 8), (8, 3), (7,), (3, 8), (8, 3), (7,)]
transform_input: 3.743e-06 (2.239e-07)
transform_input2: 3.308e-06 (2.461e-07)
transform_input3: 2.893e-06 (1.354e-07)
transform_input4: 2.848e-06 (9.448e-07)
transform_input5: 1.710e-06 (2.458e-07)
transform_input6: 1.477e-06 (1.823e-07)
[(8, 8), (8, 8)]
transform_input: 1.894e-06 (2.101e-07)
transform_input2: 1.641e-06 (1.979e-07)
transform_input3: 1.370e-06 (1.294e-07)
transform_input4: 1.593e-06 (1.063e-06)
transform_input5: 9.250e-07 (2.063e-07)
transform_input6: 8.319e-07 (1.358e-07)
[(3,), (7,), (6,)]
transform_input: 2.415e-06 (1.107e-06)
transform_input2: 1.944e-06 (2.173e-07)
transform_input3: 1.635e-06 (1.073e-07)
transform_input4: 1.710e-06 (1.585e-07)
transform_input5: 1.079e-06 (1.747e-07)
transform_input6: 1.061e-06 (9.636e-07)
[(3,), (7,), (6,), (3,), (7,), (3,)]
transform_input: 3.653e-06 (1.049e-06)
transform_input2: 3.152e-06 (2.191e-07)
transform_input3: 2.749e-06 (1.331e-07)
transform_input4: 2.576e-06 (1.495e-07)
transform_input5: 1.674e-06 (2.078e-07)
transform_input6: 1.285e-06 (1.356e-07)
[(2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2), (2, 3), (3, 2)]
transform_input: 6.527e-06 (2.066e-07)
transform_input2: 5.980e-06 (2.107e-07)
transform_input3: 5.370e-06 (1.381e-07)
transform_input4: 5.504e-06 (4.534e-06)
transform_input5: 2.974e-06 (1.017e-06)
transform_input6: 2.422e-06 (1.929e-07)
[(2,), (2,)]
transform_input: 1.830e-06 (1.616e-07)
transform_input2: 1.565e-06 (2.068e-07)
transform_input3: 1.317e-06 (1.332e-07)
transform_input4: 1.541e-06 (1.074e-06)
transform_input5: 9.414e-07 (1.911e-07)
transform_input6: 8.253e-07 (1.391e-07)
[(2,), (2,), (2,), (2,), (2,), (2,)]
transform_input: 3.798e-06 (1.423e-06)
transform_input2: 3.165e-06 (2.080e-07)
transform_input3: 2.767e-06 (1.471e-07)
transform_input4: 2.602e-06 (1.492e-07)
transform_input5: 1.718e-06 (2.056e-07)
transform_input6: 1.255e-06 (1.435e-07)
[(3, 8), (8, 3), (7,), (2, 3), (3, 2), (2, 3)]
transform_input: 3.762e-06 (2.038e-07)
transform_input2: 3.323e-06 (2.212e-07)
transform_input3: 2.923e-06 (1.341e-07)
transform_input4: 2.763e-06 (1.599e-07)
transform_input5: 1.663e-06 (2.349e-07)
transform_input6: 1.468e-06 (1.796e-07)
[(3, 8), (8, 3), (7,), (3, 4), (8, 8), (8, 8), (3,), (7,), (6,), (3,), (7,), (2, 3), (3, 2), (2,), (2,)]
transform_input: 7.692e-06 (3.647e-07)
transform_input2: 7.048e-06 (2.708e-07)
transform_input3: 6.550e-06 (1.439e-06)
transform_input4: 6.143e-06 (2.281e-06)
transform_input5: 3.450e-06 (2.875e-07)
transform_input6: 2.544e-06 (2.064e-07)
在这里,您可以找到用于生成结果的代码,并且可以使用它。
这可能更有效(不确定,请检查):
def transform_input(combination):
return list(set([tuple(sorted(elem)) for elem in combination]))
如您所知,元组是可散列的,因此您可以将它们用作dict
键。
您可能知道,在哈希表中插入和搜索是按 O(1) 顺序完成的。
因此,使用哈希表(python 中的dict
)可以帮助您改进代码。
我根据上述事实编写了transform_input2
。
我知道它的可读性不是很好,但它提高了性能。
*我稍微更改了您的代码,以便能够更好地计算每个解决方案中的经过时间。
import time
def transform_input3(combination):
print("start")
start = time.time()
res = list(set([tuple(sorted(elem)) for elem in combination]))
end = time.time()
print(end - start)
return res
def transform_input2(combination):
print("start")
start = time.time()
hashtable = {}
for t in combination:
rev = t[::-1]
if rev in hashtable:
if t[0]<rev[0]:
hashtable[(t[0],t[1])] = None
else:
continue
else:
hashtable[t] = None
res = list(hashtable.keys())
end = time.time()
print(end - start)
return res
def transform_input(combination):
print("start")
start = time.time()
combination = [list(elem) for elem in combination]
for t in combination:
t.sort()
combination = [tuple(elem) for elem in combination]
combination = list(set(combination))
end = time.time()
print(end - start)
return combination
my_input = [
(3,8), (8,3), (7,), (3,4),
(8,8), (8,8),
(3,), (7,), (6,), (3,), (7,),
(2,3), (3,2),
(2,), (2,)
]
print(transform_input(my_input))
print(transform_input2(my_input))
print(transform_input3(my_input))
这是一次执行的结果:
start
3.600120544433594e-05
[(2,), (3,), (3, 8), (8, 8), (2, 3), (6,), (7,), (3, 4)]
start
1.0967254638671875e-05
[(3, 8), (7,), (3, 4), (8, 8), (3,), (6,), (2, 3), (2,)]
start
1.9788742065429688e-05
[(2,), (3,), (3, 8), (8, 8), (2, 3), (6,), (7,), (3, 4)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.