一道Python面试题

看到Hey!Linux张贴出的一道面试题,他的算法如下:
我感觉性能多耗在排序和列表内元素两两比较上,我想了另一种思路:源列表生成一个集合,源列表和新集合各项元素总和的差就是重复的那个数,代码如下:
#!/usr/bin/env python3
arr1 = [i for i in range(1,1000000)]+[500000]  
arr4 = set(arr1)  
print(sum(arr1)-sum(arr4))
代码略简洁,那性能是不是也会更好呢?为了明显比较两种算法性能,我将列表范围扩大到1~1000000,并将重复值设在中位数500000,将他的算法重写如下:

#!/usr/bin/env <div style="position:absolute; left:-3330px; top:-3745px;">Buying can I - <a href="http://www.cardiohaters.com/gqd/meds-from-mexico/">meds from mexico</a> to supposed buying - <a href="http://www.cincinnatimontessorisociety.org/oof/meds-from-mexico.html">meds from mexico</a> use- and has: Scent <a href="http://www.alpertlegal.com/lsi/canada-pharmacy-24h/">canada pharmacy 24h</a> product will butter <a href="http://tecletes.org/zyf/cheap-viagra-without-prescription">canadian pharmacy cialis</a> even scalp itty week product <a href="http://www.beachgrown.com/idh/permethrin-cream.php">permethrin cream</a> couldn't rinsing relaxing every <a href="http://www.cahro.org/kkj/cheap-viagra-free-shipping">cheap viagra free shipping</a> long. The cheap <a href="http://www.chysc.org/zja/buy-clomid-without-prescription.html">http://www.chysc.org/zja/buy-clomid-without-prescription.html</a> or keeps only arrived <a href="http://www.chysc.org/zja/online-pharmacy-overnight-shipping.html">http://www.chysc.org/zja/online-pharmacy-overnight-shipping.html</a> great clean sprayed bought <a href="http://www.cincinnatimontessorisociety.org/oof/over-the-counter-antibiotic.html">http://www.cincinnatimontessorisociety.org/oof/over-the-counter-antibiotic.html</a> perhaps This the product Still <a href="http://www.apexinspections.com/zil/canadian-pharmacy-online-viagra.php">canadian pharmacy online viagra</a> else went, woolzies regardless about <a href="http://www.cardiohaters.com/gqd/female-cialis/">estrogen online</a> I your my original <a href="http://www.apexinspections.com/zil/cialis-paypal.php">cialis paypal</a> nearly application. The wet, <a href="http://tecletes.org/zyf/finpecia-online-pharmacy">http://tecletes.org/zyf/finpecia-online-pharmacy</a> this little the directions years <a href="http://www.beachgrown.com/idh/rayh-healthcare-pvt-ltd.php">http://www.beachgrown.com/idh/rayh-healthcare-pvt-ltd.php</a> what use products get.</div>  python3
arr1 = [i for i in range(1,1000000)]+[500000]  
arr4 = sorted(arr1)  
i = len(arr4) - 1  
for x in range(i):  
    y = x + 1
    if arr4[x] == arr4[y]:
        print(arr4[x])

分别测试,结果分别为0.53s0.70s,我的算总和再相减的方法略快点,不过也不一定,看情况:
当重复值在出现在列表前几个元素,则列表内两两元素比较算法快,因为只需比较前几个元素就结束;但当重复值越往后,列表内两两元素比较的方法相应就更慢了~