简体   繁体   中英

Inconsistent results while measuring execution time of delayed loop

I have a pretty specific problem. I want to measure execution time of the generator loop (with the yield keyword). However, I don't know in what intervals next() will be called on this generator. This means I can't just get the timestamp before and after the loop. I thought getting the timestamp at the beginning and end of each iteration will do the trick but I'm getting very inconsistent results.

Here's the test code:

import time


def gen(n):
    total = 0
    for i in range(n):
        t1 = time.process_time_ns()
        # Something that takes time
        x = [i ** i for i in range(i)]
        t2 = time.process_time_ns()
        yield x
        total += t2 - t1
    print(total)


def main():
    for i in gen(100):
        pass

    for i in gen(100):
        time.sleep(0.001)

    for i in gen(100):
        time.sleep(0.01)


if __name__ == '__main__':
    main()

Typical output for me looks something like this:

2151918
9970539
11581393

As you can see it looks like the delay outside of the loop somehow influences execution time of the loop itself.

What is the reason of this behavior? How can I avoid this inconsistency? Maybe there's some entirely different way of doing what I'm trying to achieve?

You can switch the yield x and total += t2 - t1 lines to only count the time it takes to create x.

For more in dept also see: Behaviour of Python's "yield"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM