Python's [] at least 3x faster than list()?

前端 未结 4 587
梦谈多话
梦谈多话 2020-12-28 18:37

It appears that using [] around a generator expression (test1) behaves substantially better than putting it inside of list() (test2). The slowdown isn\'t there when I simpl

4条回答
  •  死守一世寂寞
    2020-12-28 19:19

    Your test2 is roughly equivalent to:

    def test2():
        def local():
            for i in x:
                yield i
        return list(local())
    

    The call overhead explains the increased processing time.

提交回复
热议问题