2017-08-24 47 views

回答

1

经过深入研究和探索潜在的解决方案;我想出了以下解决方案:

  1. 以下函数添加到源文件和装饰原有的功能与@do_cprofile

    import cProfile 
    
    def do_cprofile(func): 
        def profiled_func(*args, **kwargs): 
         profile = cProfile.Profile() 
         try: 
          profile.enable() 
          result = func(*args, **kwargs) 
          profile.disable() 
          return result 
         finally: 
          profile.dump_stats('/tmp/profile_bin.prof') 
        return profiled_func 
    
  2. 到配置文件转换生成的/tmp/profile_bin.prof人类可读的文件

    import pstats 
    
    f = open('/tmp/human_readable_profile.prof', 'w') 
    stats = pstats.Stats('/tmp/profile_bin.prof', stream=f) 
    stats.sort_stats('cumulative').print_stats() 
    f.close()