• 测试对于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
  • 相关阅读:
    vue基础笔记
    HM.DAY-02
    前端基础(三)
    前端基础 (二)
    前端基础(一)
    爬虫基础入门(二)
    爬虫基础入门(一)
    Python进阶(三)
    Python进阶(二)
    python切片
  • 原文地址:https://www.cnblogs.com/yldf/p/11900126.html
Copyright © 2020-2023  润新知