python函数示例_timeit()函数与Python中的示例

python函数示例

Today it is very important to finish a project not only in limited resources but it is also important to finish it under the least possible period. We have time() function in Python to calculate the time of execution of code.

今天,不仅要用有限的资源来完成一个项目,而且在尽可能短的时间内完成它也很重要。 我们在Python中有time()函数来计算代码执行时间。

When we execute any code several background operations happen to execute our code execution. When time() function counts on the time it does not take into consideration background operations going on to execute this code. Also, we subtract time in starting and at the end to get the required time. Now timeit() function has been developed to find exact timing taken any particular code execution. Even it counts on timing up to .0000 decimal points of a millisecond to find exact timing taken in cod execution. Here we will see how this function works.

当我们执行任何代码时,会发生几个后台操作来执行我们的代码执行。 当time()函数依靠时间时,它不考虑正在进行的执行该代码的后台操作。 另外,我们减去开始和结束时的时间以获得所需的时间。 现在,已经开发了timeit()函数,以查找执行任何特定代码所需要的确切时间。 即使是它,也要依靠最高达0.0000毫秒的时间来找到在执行鳕鱼时需要的确切时间。 在这里,我们将看到此功能的工作原理。

Code Execution for timeit() function through an example:

通过示例的timeit()函数的代码执行:


We will type python and hit enter. You will respond like the version of python and Microsoft we are using. Also, it confirms that we are working in python.


我们将输入python并按回车键。 您将像我们正在使用的python和Microsoft版本一样进行响应。 另外,它确认我们在python中工作。

Import statement:

导入声明:

    import timeit

If you will get a response like 'import' is not defined then go and check for system variable has been updated or not.

如果您收到未定义“ import”之类的响应,请检查系统变量是否已更新。

Now, we will program a simple mathematical operation and find its execution timing by two methods.

现在,我们将对一个简单的数学运算进行编程,并通过两种方法找到其执行时间。

# python example to demonstrate the
# example of timeit() function

# importing the module
import timeit

# operations and time calcuation
print('a' *5)
print('a' + 'a' +'a' + 'a' + 'a' )
print(timeit.timeit("b = 'a' *3",number = 10000000))

Output

输出量

aaaaa
aaaaa
0.9088429469993571

Here you can put the value of number any number times as number times code execution required.

在这里,您可以将number的值乘以number number作为所需的代码执行次数。

import timeit
print(timeit.timeit("ab = 'a' + 'a' + 'a' + 'a' + 'a' ", number=10000000))

Output

输出量

0.8887228329986101

This method describes the method to find execution timing manually. Manually here means we have to perform the whole operation a number of times.

此方法描述了手动查找执行时间的方法。 在此手动进行意味着我们必须多次执行整个操作。

Automatic Repetition Method with Example

示例的自动重复方法

Now we will see how to perform the same tedious operation automatically?

现在,我们将看到如何自动执行相同的繁琐操作?

import timeit

print(timeit.repeat("b = 'a'* 3", number = 10000000,repeat = 5))
print()
print(timeit.repeat("ab = 'a' + 'a' + 'a' + 'a' + 'a' " , number = 10000000,repeat = 5))

Output

输出量

[0.8862817650006036, 0.8274680489994353, 0.891247380997811, 0.9186934919998748, 0.900538891000906]

[0.8854398910043528, 0.8779188379994594, 0.8196939810004551, 0.8731913320007152, 0.8250786080025136]

Here we can see repetitions happen a number of times automatically.

在这里,我们可以看到重复会自动发生多次。

Why timeit()?

为什么要timeit()?

timeit() function has been the best option to find execution timing than time function because of its accuracy. Many of our assumptions regarding code execution in time() function were wrong which is rectified in timeit() function like of consideration of time taken by background operations also. Similarly, timeit() is also a more precious and shortest path to find execution timing.

由于timeit()函数的准确性,它比time函数是查找执行时间的最佳选择。 我们关于time()函数中代码执行的许多假设是错误的,在timeit()函数中已得到纠正,就像考虑到后台操作所花费的时间一样。 同样, timeit()也是查找执行时间的更宝贵和最短的路径。

翻译自: https://www.includehelp.com/python/timeit-function-with-example.aspx

python函数示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值