Python 测线探查器未按预期工作

Python 测线探查器未按预期工作,python,profiling,Python,Profiling,正在尝试将line_profiler用作API。在和之后(向下滚动到行分析),我得到了一个用于分析一些numpy UFUNC的最低限度测试用例: import numpy as np import line_profiler import time shp = (1000,1000) a = np.ones(shp) o = np.zeros(shp) def main(): t = time.time() np.divide(a,1,o) for i in xran

正在尝试将line_profiler用作API。在和之后(向下滚动到行分析),我得到了一个用于分析一些numpy UFUNC的最低限度测试用例:

import numpy as np
import line_profiler
import time

shp = (1000,1000)
a = np.ones(shp)
o = np.zeros(shp)

def main():
    t = time.time()
    np.divide(a,1,o)
    for i in xrange(200):
        np.multiply(a,2,o)
        np.add(a,1,o)
    print 'duration', time.time()-t

profiler = line_profiler.LineProfiler()
profiler.add_function(main)
main()
profiler.print_stats()
我在stdout中得到了这一点,它表明
main
run,但没有分析:

duration 2.6779999733
Timer unit: 5.59936e-07 s

File: testprof.py
Function: main at line 9
Total time: 0 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     9                                           def main():
    10                                               t = time.time()
    11                                               np.divide(a,1,o)
    12                                               for i in xrange(200):
    13                                                   np.multiply(a,2,o)
    14                                                   np.add(a,1,o)
    15                                               print 'duration', time.time
()-t
我是line_profiler的新手。查看我是否好奇为什么不使用cProfile。

尝试添加

profiler.enable_by_count()

以前

main()


我找到了一个解决方法,但仍然想知道是否有人可以或不能复制此方法,以查看我的设置是否存在问题。解决方法:使用
profiler.run('main()')
而不是
main()
profiler.run('main()')
-它也适用于我。谢谢