Python数据分析(中英对照)·Measuring Time 测量时间
2.4.4: Measuring Time 测量时间
It is often helpful to be able to measure how long a segment of code
能够测量代码段的长度通常很有帮助
takes to run.
跑步需要时间。
One reason for wanting to know might be that you
想知道的一个原因可能是你
have two or more ways of coding up the same task
有两种或两种以上的方法对同一任务进行编码
and you’d like to know which one is faster.
你想知道哪一个更快。
Another reason might be that you have a large dataset
另一个原因可能是您的数据集很大
and you’d like to have a sense ahead of time
你想提前有个感觉
how long it might take to run your code.
运行代码可能需要多长时间。
You could do this by running the code for smaller datasets first, time the
您可以通过首先为较小的数据集运行代码来实现这一点
running time, and then extrapolate from there to the running
运行时间,然后从那里推断到运行时间
time for the whole dataset.
整个数据集的时间。
A simple way to measure time in Python is to use the time module.
在Python中测量时间的一种简单方法是使用时间模块。
We will import the module by saying import time.
我们将通过说出导入时间来导入模块。
One of the most useful functions in the time module is the time.clock function.
时间模块中最有用的功能之一是time.clock功能。
That gives us the current time.
这给了我们当前的时间。
We can capture that in a variable called start time.
我们可以在一个名为“开始时间”的变量中捕获它。
So we just type start_time equals time.clock.
所以我们只需输入start_time等于time.clock。
And then we can run this again to extract the end time.
然后我们可以再次运行它来提取结束时间。
I’m going to call that end time.
我要称之为结束时间。
So end_time equals time.clock.
所以结束时间等于时间。
If I want to see how much time has elapsed between these two time points,
如果我想知道这两个时间点之间经过了多少时间,
I can take my end time and from that, I will subtract my start time.
我可以计算我的结束时间,从中减去我的开始时间。
There are a few different ways to measure time in Python and each of them
在Python中有几种不同的时间度量方法,每种方法都有
has its uses.
有它的用途。
But for many purposes, such as comparing performance,
但出于许多目的,例如比较性能,
time.clock is the right function to use.
time.clock是正确使用的函数。
Working with our previous example, the one that relied only on pure Python,
使用我们前面的示例,即仅依赖纯Python的示例,
I have the code here in front of me.
我面前有密码。
In order to time its performance, I’m first
为了给它的表演计时,我是第一个
going to catch the start time by saying start_time equals time.clock.
通过说start_time等于time.clock来捕捉开始时间。
The code will run
代码将运行
and once it’s finished, I went to capture the end time.
一旦完成,我就去捕捉结束时间。
So I capture time here again.
所以我再次在这里捕捉时间。
And then what I’d like to do is print the difference between end time
然后我要做的是打印结束时间和
and start time.
开始时间。
Let’s try running this code.
让我们试着运行这段代码。
In this case, we might expect this to take somewhere between 5 seconds
在这种情况下,我们可能需要5秒左右的时间
and perhaps up to 1 minute, depending on the speed of your computer.
根据你电脑的速度,可能最多1分钟。
Let’s then look at the second implementations of this example,
然后让我们看一下这个示例的第二个实现,
the one that relies on NumPy.
那个依赖NumPy的人。
Again, we will capture start time,
同样,我们将捕获开始时间,
and will the same once the code has run.
并且在代码运行后也会发生同样的情况。
In this case, we just have two lines for the code.
在本例中,我们只有两行代码。
And in the end, we just want to print out the difference between end time
最后,我们只想打印出结束时间之间的差异
and start time.
开始时间。
We would expect this code to run much faster because it makes use of NumPy.
我们希望这段代码运行得更快,因为它使用了NumPy。
In this case, the code runs almost immediately.
在这种情况下,代码几乎立即运行。
Let’s look at the difference in performance
让我们看看性能上的差异
between these two different ways of implementing the same example.
在实现同一示例的这两种不同方法之间。
I’m going to take the time I got from the first example, the pure Python
我将使用第一个示例中的时间,纯Python
implementation, and I will then divide that by the time I got using NumPy.
实现,然后我将把它除以我使用NumPy时得到的值。
In this case, we see that the NumPy implementation is over 80 times faster
在本例中,我们看到NumPy实现的速度快了80多倍
than the Python-based implementation.
而不是基于Python的实现。
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Measuring Time 测量时间
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
- 是否提供免费更新服务?
- 持续更新,永久免费
- 是否经过安全检测?
- 安全无毒,放心食用