Measuring the time it takes for a function to run and complete in Python

后端 未结 3 1397
悲哀的现实
悲哀的现实 2021-01-13 03:01

In Python 3.4.1, I\'m trying to measure how long it takes for a function to run and complete then recording it. I\'m currently doing it this like so:

startti         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-13 03:59

    I'll admit that I'm not very familiar with Python's asyncio, but I believe the issue is not in your timing, but in your useage of asyncio.

    I think you are just creating a future with the value of method(), however that is all that you are timing: the actual creation of this promise.

    You are not timing the actual evaluation of the future value. This is why timing sleep(3) and method() take roughly the same amount of time.

    I suggest trying to change asyncio.wait_for((method()), 5) with yield from asyncio.wait_for((method()), 3) or just timing method() if you can.

提交回复
热议问题