Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将cProfile结果保存到可读的外部文件_Python_Python 3.x_Text_Profiling_Cprofile - Fatal编程技术网

Python 将cProfile结果保存到可读的外部文件

Python 将cProfile结果保存到可读的外部文件,python,python-3.x,text,profiling,cprofile,Python,Python 3.x,Text,Profiling,Cprofile,我正在使用cProfile尝试分析我的代码: pr = cProfile.Profile() pr.enable() my_func() # the code I want to profile pr.disable() pr.print_stats() 但是,结果太长,无法在Spyder终端中完全显示(无法看到运行时间最长的函数调用…)。我还尝试使用 cProfile.run('my_func()','profile_results') 但是输出文件不是人类可读的格式(尝试使用和不使

我正在使用
cProfile
尝试分析我的代码:

pr = cProfile.Profile()
pr.enable()
my_func()   # the code I want to profile
pr.disable()
pr.print_stats()
但是,结果太长,无法在Spyder终端中完全显示(无法看到运行时间最长的函数调用…)。我还尝试使用

 cProfile.run('my_func()','profile_results')
但是输出文件不是人类可读的格式(尝试使用和不使用
.txt
后缀)


因此,我的问题是,如何将分析结果保存到一个人类可读的外部文件中(如以
.txt
格式,正确显示所有单词)?

已更新。您可以使用获取分析程序的输出并将其保存到文件中。 以下是一个例子:

import cProfile
import pstats
import io


def my_func():
    result = []
    for i in range(10000):
        result.append(i)

    return result

pr = cProfile.Profile()
pr.enable()

my_result = my_func()

pr.disable()
s = io.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats('tottime')
ps.print_stats()

with open('test.txt', 'w+') as f:
    f.write(s.getvalue())
运行脚本并打开
test.txt
。您将看到可读的结果:

   10002 function calls in 0.003 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.002    0.002    0.003    0.003 /path_to_script.py:26(my_func)
    10000    0.001    0.000    0.001    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
我也可以推荐使用+。下面是一个如何使用它的示例。文件结构:

# test_ex.py - just a small web app
import cProfile
import json
from functools import wraps
from flask import Flask

from example.mod1 import func1
from example.mod2 import func2

app = Flask(__name__)

# profiling decorator
def profiling():

    def _profiling(f):
        @wraps(f)
        def __profiling(*rgs, **kwargs):
            pr = cProfile.Profile()
            pr.enable()

            result = f(*rgs, **kwargs)

            pr.disable()
            # save stats into file
            pr.dump_stats('profile_dump')

            return result
        return __profiling
    return _profiling

# demonstration route with profiler
@app.route('/test')
@profiling()
def test():
    counter = func1()
    dict_data = func2()
    result = dict()

    for key, val in dict_data.items():
        result[key] = val + counter

    return json.dumps(result)


if __name__ == '__main__':
    app.run(debug=True, port=8083)
示例包-让我们假设这是某种应用程序逻辑

# example.mod1
def func1():
    counter = 0

    for i in range(100000):
        counter += i

    return counter

# example.mod2
def func2():
    res = dict()

    for i in range(300000):
        res['key_' + str(i)] = i

    return res
现在让我们运行服务器(
python3test\u ex.py
)并打开
http://localhost:8083/test
。几秒钟后,您将看到长json。之后,您将在项目文件夹中看到profile\u dump文件。现在在项目文件夹中运行python live解释器,并使用pstats打印转储文件:

import pstats
p = pstats.Stats('profile_dump')
# skip strip_dirs() if you want to see full path's
p.strip_dirs().print_stats()

您还可以轻松地对结果进行排序:

p.strip_dirs().sort_stats('tottime').print_stats()
p.strip_dirs().sort_stats('cumulative').print_stats()
p.strip_dirs().sort_stats().print_stats('mod1')

希望这有帮助。

在前面的答案的基础上展开,您可以将所有内容转储到.csv文件中,以便在您喜爱的电子表格应用程序中进行排序和处理

import pstats,StringIO

# print stats to a string
result=StringIO.StringIO()
pstats.Stats(filename,stream=result).print_stats()
result=result.getvalue()

# chop the string into a csv-like buffer
result='ncalls'+result.split('ncalls')[-1]
result='\n'.join([','.join(line.rstrip().split(None,6)) for line in result.split('\n')])

# save it to disk
f=open(filename.rsplit('.')[0]+'.csv','w')
f.write(result)
f.close()

您实际上不需要StringIO,因为文件符合流的条件

import pstats

with open("profilingStatsAsText.txt", "w") as f:
    ps = pstats.Stats("profilingResults.cprof", stream=f)
    ps.sort_stats('cumulative')
    ps.print_stats()
你可以用。在Python 3.8中:

with cProfile.Profile() as pr:
    my_func()  

pr.dump_stats('/path/to/filename.prof')

您可以运行探查器,将输出保存到文件中,如下所示:

import cProfile

cProfile.run('my_func()', 'profile_results')
然后使用类
pstats.Stats
()格式化结果:


基本上,cProfile输出是一个二进制文件。为什么您不能使用
dump\u stats
+
pstats.stats
?@DanilaGanchar您的意思是只将结果打印到终端?我已经尝试过了,但是日志太长了,通常最重要的部分(例如,如果我使用“cumtime”对它们进行排序,则需要花费最长时间的函数调用)都在顶部,没有显示出来。另外,我想保存结果,以便比较不同版本的代码更容易。ohh nice的可能副本不知道stringio obj。getvalue()可以像那样复制到.txt。Thnx@thomas.mac很乐意帮忙;)您可以使用类似的工具,指向
.prof
文件以获得一些漂亮的图形
import pstats

file = open('formatted_profile.txt', 'w')
profile = pstats.Stats('.\profile_results', stream=file)
profile.sort_stats('cumulative') # Sorts the result according to the supplied criteria
profile.print_stats(15) # Prints the first 15 lines of the sorted report
file.close()