I have a child
function that will iterate over 1000000
times. Now when I call that child function on two different functions. It's showing different time is taken for every execution. Why this is happening?
import timeit
def child():
for i in range(1_000_000):
yield i
def slow():
yield from child()
def fast():
yield from child()
baseline = timeit.timeit(
stmt='for _ in slow(): pass',
globals=globals(),
number=50
)
comparision = timeit.timeit(
stmt='for _ in fast(): pass',
globals=globals(),
number=50
)
print(f'Manual nesting {baseline:.2f}s')
print(f'Composed nesting {comparision:.2f}s')
Output
# First execution
Manual nesting 9.94s
Composed nesting 9.35s
# Second execution
Manual nesting 9.20s
Composed nesting 9.77s
As you have already seen the above output. It's showing different time intervals. Why does it happen for the same process?