saving cProfile results to readable external file

后端 未结 5 1774
清酒与你
清酒与你 2021-02-19 01:16

I am using cProfile try to profile my codes:

pr = cProfile.Profile()
pr.enable()
my_func()   # the code I want to profile
pr.disable()
pr.print_stat         


        
5条回答
  •  时光说笑
    2021-02-19 02:05

    You don't really need StringIO, as a file qualifies as a stream.

    import pstats
    
    with open("profilingStatsAsText.txt", "w") as f:
        ps = pstats.Stats("profilingResults.cprof", stream=f)
        ps.sort_stats('cumulative')
        ps.print_stats()
    

提交回复
热议问题