zoukankan      html  css  js  c++  java
  • 测试对于list的sort与sorted的效率

    sorted

    from time import clock
    from random import randint
    start = clock()
    a = [randint(0,1000000) for i in range(1000000)]
    # a.sort()
    a = sorted(a)
    end = clock()
    print a
    print "time cost is %f s" %((end-start)*10)

    结果:
    time cost is 35.358285 s*

    sort

    from time import clock
    from random import randint
    start = clock()
    a = [randint(0,1000000) for i in range(1000000)]
    a.sort()
    # a = sorted(a)
    end = clock()
    print a
    print "time cost is %f s" %((end-start)*10)

    结果:
    time cost is 34.877686 s

    总结

    • 对于 list 而言,sort() 比 sorted() 效率要高
    • 但是 sorted() 用途更广,可以用于任何可迭代容器;而 sort() 只能用于 list
  • 相关阅读:
    Codeforces_739_B
    Codeforces_732_D
    D
    C
    E
    商汤AI园区的n个路口(中等)
    D. The Fair Nut and the Best Path
    HDU6446
    分解质因数(线性筛)
    D. Extra Element
  • 原文地址:https://www.cnblogs.com/yldf/p/6249861.html
Copyright © 2011-2022 走看看