Measure Code Execution Time Accurately in Python

栏目: IT技术 · 发布时间: 4年前

内容简介:Measuring code execution times is hard. Learn how to eliminate systematic and random measurement errors and obtain more reliable results.We often need to measure how long a specific part of code takes to execute. Unfortunately, simply measuring the system

Measuring code execution times is hard. Learn how to eliminate systematic and random measurement errors and obtain more reliable results.

We often need to measure how long a specific part of code takes to execute. Unfortunately, simply measuring the system time before and after a function call is not very robust and susceptible to systematic and random measurement errors. This is especially true for measuring very short intervals (< 100 milliseconds).

Systematic and Random Errors

So what is wrong with the following way of measuring?

time_start = time.time()
my_function()
time_end = time.time()
execution_time = time_end - time_start

First, there is a systematic error : by invoking time.time() , an unknown amount of time is added to the execution time of my_function() . How much time? This depends on the OS, the particular implementation and other uncontrollable factors.

Second, there is a random error : the execution time of the call to my_function() will vary to a certain degree.

We can combat the random error by just performing multiple measurements and taking the average of those. However, it is much more challenging to remove the systematic error.

Straight Line Fitting

Carlos Moreno and Sebastian Fischmeister presented a novel technique to combat this systematic error. The basic idea is to first measure the time of one function call, then the time of two , then the time of three, and so on. The resulting method may look like this:

time_1 = time.time()
my_function()
time_2 = time.time()
my_function()
my_function()
time_3 = time.time()
my_function()
my_function()
my_function()
time_4 = time.time()
# ...

You can then fit a straight line through the measurements:

Measure Code Execution Time Accurately in Python

The overall execution time can then be obtained by taking the slope a from the straight line y = a x + b .

In the above example, the straight line is y = 205.91 x + 29.56 ; therefore, the execution time equals 205.91 milliseconds.

The authors note that this type of measurement is very robust against occasional measurements with large errors. This can be visualized by artificially changing the 4th measurement and rerunning the line fitting process:

Measure Code Execution Time Accurately in Python

Even though one value is completely off, the resulting slope (201.15) is still very close to the previously measured value.

To learn more about the mathematical basics of this method, I invite you to read the original paper: https://uwaterloo.ca/embedded-software-group/sites/ca.embedded-software-group/files/uploads/files/ieee-esl-precise-measurements.pdf

Python Implementation

You can find my implementation of the presented algorithm in my public GitLab repository:

https://gitlab.com/bernhard.knasmueller/accurate-time-measurements-python

All credit for the algorithm and the idea goes to Moreno and Fischmeister.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

精益思想

精益思想

(美)詹姆斯 P.沃麦克(James P.Womack)、(英)丹尼尔 T.琼斯(Daniel T.Jones) / 沈希瑾、张文杰、李京生 / 机械工业出版社 / 2011-4 / 48.00元

打算尝试精益的人,该怎么做? 已经实行精益的人,下一步怎么办? 本书包含了最新的精益理论、方法和工具,一一解答上述问题。 这是目前关于流程再造最好的书,也是最好读的。——《高业周刊》 本书中文简体字版由FreePress通过AiWA授权机械工业出版社在中国大陆独家出版发行。未经出版者书面许可,不得以任何方式抄袭、复制或节录本书中的任何部分。 《精益思想》于1996年秋......一起来看看 《精益思想》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具