python - 提高 python 代码的速度性能
问题描述
该函数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))
输出是:
[(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
函数(即,使用迭代器)。
def transform_input4(combination):
return list(set(map(tuple, map(sorted, combination))))
如果内部元组不能包含超过 2 个元素,您可以实现类似这样的操作,以避免对它们中的每一个进行排序并从list
to转换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使用的)添加了一些项目,并对所有方法进行了基准测试,在每个项目上运行每个函数 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)
在这里,您可以找到用于生成结果的代码,并且可以使用它。
推荐阅读
- node.js - Expressjs中使用passport-google-token刷新令牌
- python - 如何使用循环 Pandas 向列添加值
- c# - 字符串中的字符索引错误
- nginx - 如何让 NGINX 反向代理和应用程序
- python - PySide2 Qt 与系统中的其他 Qt 发生冲突?
- css - 显示两个符合 React 的 div
- google-cloud-platform - 如何在 2 个项目之间移动 Google Cloud DNS 条目?
- javascript - window.prompt() 破坏文本
- firebase - 如何在 angularfire 中执行交易?
- ios - 从相机录制时,AVAsset writer 在开始时出现口吃