admin管理员组文章数量:1386790
I stormed up a Youtube short which introduced me to repeat
from the itertools
.
I then decided to compare it to range
and repeat
but it seem very slow.
What could be the reason?
Below is my code
from itertools import repeat
from timeit import timeit
def range_generator(start, stop, step=1):
while start <= stop:
yield start
start += step
def for_range():
for _ in range(999_999_999): ...
def for_repeat():
for _ in repeat(None, 999_999_999): ...
def for_range_gen():
for _ in range_generator(1, 999_999_999): ...
range_time = timeit(for_range, number=1)
repeat_time = timeit(for_repeat, number=1)
for_range_gen_time = timeit(for_range_gen, number=1)
print(f"{range_time:.3f}s")
print(f"{repeat_time:.3f}s")
print(f"{for_range_gen_time:.3f}s")
I tried comparing and timing them all and it turned out my custom generator is way too slow compared to range
and repeat
.
Running the script 3 different times, i got;
28.492s
17.067s
121.059s
--------
32.078s
16.321s
148.467s
---------
29.349s
18.025s
126.159s
ChatGPT seem to let me know range
and repeat
are both implemented in C that's why it much faster than my custom generator. How true is this please?
I expect my custom generator should finish way faster than other functions range_time
and repeat_time
.
本文标签: memory managementwhy is python generator slower than range and itertool repeatStack Overflow
版权声明:本文标题:memory management - why is python generator slower than range and itertool repeat? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744503486a2609454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论