KCacheGrind es una herramienta de visualización de rendimiento, necesita leer un archivo de formato fijo para obtener los gráficos de visualización, no es exclusivo de Python
Anteriormente usamos cProfile para el análisis de rendimiento, y luego usamos cProfile para analizarlo y guardarlo como un archivo. El archivo no puede ser
leído directamente por KCacheGrind . Necesita usar la herramienta pyprof2calltree para convertir. El archivo obtenido está disponible para KCacheGrind, y luego Puede obtener resultados visuales.
Por lo tanto, es necesario preparar dos herramientas a continuación:
(1) KCacheGrind
Esto se puede descargar directamente: https://sourceforge.net/projects/qcachegrindwin/
Debido a que hay muchas versiones de KCacheGrind, para facilitar la visualización de los resultados (porque no hay escritorio en mi linux), utilizo la versión win. Después de la descarga, se puede usar después de la descompresión, sin instalación.
(2) pyprof2calltree
La instalación es sencilla
root@root:/opt/pypy3.7-v7.3.2-linux64# pip3 install pyprof2calltree
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pyprof2calltree
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ca/2a/e9a76261183b4b5e059a6625d7aae0bcb0a77622bc767d4497148ce2e218/pyprof2calltree-1.4.5.tar.gz
Building wheels for collected packages: pyprof2calltree
Running setup.py bdist_wheel for pyprof2calltree ... done
Stored in directory: /root/.cache/pip/wheels/1f/d5/22/e302f7ad85b7bef4f591f7e9b75c681ceea602400bacd9fcdc
Successfully built pyprof2calltree
Installing collected packages: pyprof2calltree
Successfully installed pyprof2calltree-1.4.5
Luego proceda con todo el proceso:
(1) Obtenga el archivo .prof
import cProfile
import pstats
import sys
def run():
a = [1]*100
b = [x**3 for x in a ]
c = [x for x in b]
d = c*2
profiler = cProfile.Profile()
profiler.enable()##start
run()
profiler.create_stats()
stats = pstats.Stats(profiler)
stats.strip_dirs().sort_stats('cumulative').dump_stats('run.prof')#保存为run.prof
(2) Convertir a un archivo legible por KCacheGrind
Después de que la operación anterior sea exitosa, se obtiene un archivo llamado run.prof
Siguiente necesita ser convertido:
pyprof2calltree -i run.prof
Explicación de parámetros:
root@root:/opt/pypy3.7-v7.3.2-linux64# pyprof2calltree -h
usage: pyprof2calltree [-h] [-o output_file_path] [-i input_file_path] [-k]
[-r ...]
optional arguments:
-h, --help show this help message and exit
-o output_file_path, --outfile output_file_path
Save calltree stats to <outfile>
-i input_file_path, --infile input_file_path
Read Python stats from <infile>
-k, --kcachegrind Run the kcachegrind tool on the converted data
-r ..., --run-script ...
Name of the Python script to run to collect profiling
data
El comando anterior no usa -o, es decir, si no se especifica ningún nombre, se generará un archivo run.prof.log
Esto se puede especificar usando:
-o 名称
Y si KCacheGrind está instalado en esta máquina, puede usar el parámetro -k, que abrirá automáticamente KCacheGrind
Pero copié el archivo run.prof.log para ganar y luego lo leí:
Finalmente, adjunte el manual de instrucciones de KCacheGrind: https://docs.kde.org/trunk5/en/kdesdk/kcachegrind/kcachegrind.pdf