提高一个python代码的速度性能

问题描述 投票:0回答:2

职能 transform_input 做到以下几点:

  • 该项目的目的是: combination 参数是一个包含1或2个值的元组列表(这个列表的大小通常最大为6)。
  • 然后,它将删除所有在 combination. 我所说的冗余是指删除重复的内容,删除包含相同值交换的tuple。冗余元组的例子。(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?

这段代码在我的实际程序中执行得很频繁,如果能改进就更好了。

python python-3.x list tuples
2个回答
2
投票

这里有两种其他可能的方法;第一种是与以下作者发布的答案稍有不同 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))))

如果内部的tuple不能包含超过2个元素,你可以实现类似这样的功能,以避免对每个元素进行排序和从 listtuple:

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的答案,也是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)

这里 你可以找到用于产生结果的代码,你可以玩它。


1
投票

这可能会更有效一些(不确定,检查一下)。

def transform_input(combination):
  return list(set([tuple(sorted(elem)) for elem in combination]))

1
投票

你知道元组是可以哈希的 所以你可以把它们当作 dict 键。

你可能知道,在hashtables中插入到和搜索是以O(1)的顺序进行的。

所以使用hashtable(dict 在python中)可能会帮助你的代码得到改进。

我写了 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)]
© www.soinside.com 2019 - 2024. All rights reserved.