Python大数据分析学习.测试程序执行速度

Here, I introduce 2 magic functions which could only be operated in ipython
console:

The first is %timeit

```code
%timeit 100**3
Output[1]: 22.7 ns ± 0.897 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
[/code]

The second is %lprun

If you desire to utilize '%lprun' magic function to time your codes, you need
to install line_profiler in advance.

try:

​```code
conda install line_profiler
[/code]

then, you should do 2 steps as following:

​```code
%load_ext line_profiler
[/code]

​```code
%lprun -f function function(para)
[/code]

Now, let's test:

​```code
def test(num):
for i in range(num):
print(num)
print(str(num))
print(num*2)
return 0

%lprun -f test test(10)

Output [1]:
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 def test(num):
2 11 45.0 4.1 0.2 for i in range(num):
3 10 7493.0 749.3 36.3 print(num)
4 10 6816.0 681.6 33.1 print(str(num))
5 10 6263.0 626.3 30.4 print(num*2)
6 1 1.0 1.0 0.0 return 0
[/code]


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210608151750993.gif)
```

posted on 2021-06-29 17:41  BabyGo000  阅读(88)  评论(0)    收藏  举报