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

前端 未结 4 552
梦谈多话
梦谈多话 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:03

    list(e for e in x) isn't a list comprehension, it's a genexpr object (e for e in x) being created and passed to the list factory function. Presumably the object creation and method calls create overhead.

提交回复
热议问题