为什么以下逻辑的性能为零?

时间:2017-09-09 17:15:14

标签: python performance-testing correctness

我为codility编写了这个函数,性能为0,正确率为100%。

A = [-1, -3]
B = [1,2,3]
C = [1,4,5,6,77,2]

下面的函数应该返回最小的整数但不存在传递给它的int列表。

def solution(A):
    temp = 0;
    tempLst = []
    for item in A:
        temp = temp+1
        if temp not in A:
            tempLst.append(temp)

    return min(tempLst) if tempLst else max(A) + 1

为什么会这样?我想要的只是没有堆,itertools,partial。

1 个答案:

答案 0 :(得分:0)

你有一个有效的嵌套循环。

for item in A:

此行循环遍历A的所有元素。

    if temp not in A:

...对于每个元素,您再次循环,将tempA的每个元素进行比较。

此代码的运行时间是A大小的二次方。例如。如果A有1000个元素,则此代码需要1000 * 1000 = 1,000,000步才能完成。