Generator is pretty faster than list

 Generator iterates just once. List stores values. You choose one by your own case.

>>> timeit.timeit('(i for i in range(1000))',number=10000)
0.009893075991520561
>>> timeit.timeit('[i for i in range(1000)]',number=10000)
0.4327252675073794
>>> timeit.timeit('(i for i in range(10000))',number=10000)
0.01080568787727998
>>> timeit.timeit('[i for i in range(10000)]',number=10000)
4.4710091782229995
>>> timeit.timeit('(i for i in range(100000))',number=10000)
0.008112851248995412
>>> timeit.timeit('[i for i in range(100000)]',number=10000)
50.3096059506083



python - Generator Expressions vs. List Comprehension - Stack Overflow

コメント

人気の投稿