使用 Profile 或 cProfile 我能得到什么

Lea*_*Xue 2 python profiling

与这样的东西:

def time_this(func):
    @functools.wraps(func)
    def what_time_is_it(*args, **kwargs):
        start_time = time.clock()
        print 'STARTING TIME: %f' % start_time
        result = func(*args, **kwargs)
        end_time = time.clock()
        print 'ENDING TIME: %f' % end_time
        print 'TOTAL TIME: %f' % (end_time - start_time)
        return result
     return what_time_is_it
Run Code Online (Sandbox Code Playgroud)

我这么问是因为对我来说编写这样的描述符似乎更容易、更清晰。我认识到 profile/cprofile 尝试估计字节码编译时间等(并从运行时间中减去这些时间),所以更具体地说。

我想知道:

  • a) 编译时间什么时候变得足够重要以至于这些差异变得重要?
  • b) 我如何编写自己的分析器来考虑编译时间?

gab*_*ous 5

Profile 比 cProfile 慢,但支持线程。

cProfile 速度要快得多,但据我所知,它不会分析线程(仅分析主线程,其他线程将被忽略)。