我有代码,我想找到它的慢部分
通常的方法是: 运行此代码并以表的形式创建一个摘要,显示函数调用所用的时间。 按钮上的示例
我的代码着色想法: 运行代码并获取摘要,但在本例中,摘要是相同的代码,但每行都用不同的颜色。例如
这就是它的样子:
我认为这种代码着色非常有用,因为它是一种直观的视觉反馈,无需阅读繁琐的表格。 特别是对于更大的项目,这将是美妙的
所以我的问题是:这个想法是否已经在某处实施了? 例如,某种github项目或ide插件。我在网上找不到任何东西
下面是一个承诺的常规方法示例:
这可以通过timit
来实现
https://docs.python.org/3.7/library/timeit.html#module-timeit
或者使用python分析器。
https://docs.python.org/3.7/library/profile.html
让我的代码是:
def func1():
for i in range(0,500):
print("hello1")
def func2():
for i in range(0,50000):
print("hello2")
def func3():
for i in range(0,500000):
print("hello3")
func1()
func1()
func1()
func2()
func1()
func3()
func1()
我可以制作这样的报告:
import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
def func1():
for i in range(0,500):
print("hello1")
def func2():
for i in range(0,50000):
print("hello2")
def func3():
for i in range(0,500000):
print("hello3")
func1()
func1()
func1()
func2()
func1()
func3()
func1()
pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
给我终端输出:
552508 function calls in 3.080 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
552500 2.923 0.000 2.923 0.000 {built-in method builtins.print}
1 0.141 0.141 2.749 2.749 idea.py:12(func3)
1 0.015 0.015 0.315 0.315 idea.py:9(func2)
5 0.001 0.000 0.016 0.003 idea.py:6(func1)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
目前没有回答
相关问题 更多 >
编程相关推荐