关于for循环的速度优化


用列表推导式代替for循环创建列表

@timmer
def test1():
    a = []
    for i in range(100000):
        a.append(i)
    # print(a)

@timmer
def test2():
    a = [i for i in range(100000)]
    # print(a)
函数:test1() 开始运行:
函数: test1() 运行了 0.006981849670410156秒

函数:test2() 开始运行:
函数: test2() 运行了 0.003988742828369141秒

速度提高了很多

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM