Python Profilers

line_profiler

可逐行分析代码的占用时间情况。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env python
import requests
from bs4 import BeautifulSoup

# 这个装饰器会告诉行分析器 
# 我们想要分析这个函数
@profile
def get_urls():
    response = arequests.get('https://www.csdn.net/')
    s = BeautifulSoup(response.content, 'lxml')
    urls = []
    for url in s.find_all('a'):
        urls.append(url['href'])

if __name__ == '__main__':
    get_urls()

memory_profiler

可以分析代码占用内存的情况。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from memory_profiler import profile

@profile
def my_func():
    a = [1] * (10 ** 6)
    b = [2] * (2 * 10 ** 7)
    del b
    return a

if __name__ == '__main__':
    my_func()
Built with Hugo
Theme Stack designed by Jimmy
visitors: total visits: time(s) reads: time(s)