我们知道为了提高代码的运行速度,我们需要对书写的python
代码进行性能测试,而代码性能的高低的直接反馈是电脑运行代码所需要的时间。这里将介绍四种常用的测试代码运行速度的方法。
第一种:使用time
模块对代码的运行时间进行统计,代码如下:
<code class="prism language-python has-numbering"><span class="token keyword">import time <span class="token keyword">class <span class="token class-name">Debug<span class="token punctuation">: <span class="token keyword">def <span class="token function">mainProgram<span class="token punctuation">(self<span class="token punctuation">)<span class="token punctuation">: start_time <span class="token operator">= time<span class="token punctuation">.time<span class="token punctuation">(<span class="token punctuation">) <span class="token keyword">for i <span class="token keyword">in <span class="token builtin">range<span class="token punctuation">(<span class="token number">100<span class="token punctuation">)<span class="token punctuation">: <span class="token keyword">print<span class="token punctuation">(i<span class="token punctuation">) end_time <span class="token operator">= time<span class="token punctuation">.time<span class="token punctuation">(<span class="token punctuation">) <span class="token keyword">print<span class="token punctuation">(f<span class="token string">"the running time is: {end_time - start_time} s"<span class="token punctuation">) <span class="token keyword">if __name__ <span class="token operator">== <span class="token string">"__main__"<span class="token punctuation">: main <span class="token operator">= Debug<span class="token punctuation">(<span class="token punctuation">) main<span class="token punctuation">.mainProgram<span class="token punctuation">(<span class="token punctuation">)</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
www#gaodaima.com来源gao($daima.com搞@代@#码(网搞代码
我们采用time
模块给所要测试的代码的前后加上时间戳,一个记为start_time
,一个记作end_time
,最后代码块的运行时间为end_time-start_time
,单位为s(秒)
。当然在python
中还有许多的记录时间的模块,这里不做过多讨论,均类似于time
模块,实现思路上一致,代码实现上大同小异。
第二种:使用IPython
的Built-in magic commands
,%time
,代码如下:
<code class="prism language-python has-numbering"><span class="token keyword">class <span class="token class-name">Debug<span class="token punctuation">: <span class="token keyword">def <span class="token function">mainProgram<span class="token punctuation">(self<span class="token punctuation">)<span class="token punctuation">: <span class="token operator">%time <span class="token keyword">for i <span class="token keyword">in <span class="token builtin">range<span class="token punctuation">(<span class="token number">100<span class="token punctuation">)<span class="token punctuation">: <span class="token keyword">print<span class="token punctuation">(i<span class="token punctuation">) main <span class="token operator">= Debug<span class="token punctuation">(<span class="token punctuation">) main<span class="token punctuation">.mainProgram<span class="token punctuation">(<span class="token punctuation">) <span class="token triple-quoted-string string">""" Wall time: 1.99 ms """</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
这个类定义是可以去掉的,并不会影响最终的结果,%time
后面加上想要计算时间的代码,然后编译器就会在运行后自动给出所测试代码的运行时间,但是经过测试,%time
方法测出的时间并不准确,时间波动范围非常大,这个是很好理解的,因为计算机每时每刻都在处理一些进程,也就是说计算机的运行状态每时每刻都是不同的,所以在不同的时刻测试同一段代码的运行时间也会得到不同的结果。