三种方法分析代码性能
# 三种方式来分析代码
# 1.Python位置模块dis来查看编译出的CPU指令
import dis
code = """
a = 5
a += 1
"""
print(dis.dis(code))
# 2.使用码农高天的viztracer来分析
# https://github.com/gaogaotiantian/viztracer
'''
Basic Usage
viztracer my_script.py arg1 arg2
A result.json file will be generated, which you can open with vizviewer
vizviewer will host an HTTP server on http://localhost:9001. You can also open your browser and use that address.
If you do not want vizviewer to open the webbrowser automatically, you can use
vizviewer --server_only result.json
If you just need to bring up the trace report once, and do not want the persistent server, use
vizviewer --once result.json
vizviewer result.json
# You can display all the files in a directory and open them in browser too
vizviewer ./
# For very large trace files, try external trace processor
vizviewer --use_external_processor result.json
You can also generate standalone html file
viztracer -o result.html my_script.py arg1 arg2
'''
# 3.使用第三方库heartrate来分析代码的调用情况
# 在代码开头加入下面的即可
from heartrate import trace
trace(browser=True)
效果截图
dis.dis()
viztracer
详见官网:https://github.com/gaogaotiantian/viztracer
heartrate