python - line_profiler 未按预期工作

标签 python profiling

尝试使用 line_profiler 作为 API。正在关注their docsthis tutorial (向下滚动到线路分析),我得到了一个用于分析一些 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()

我在标准输出中得到这个,这表明 main 已运行,但未进行分析:

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 的新手。看我的other q如果好奇为什么我不使用 cProfile。

最佳答案

尝试添加

profiler.enable_by_count()

之前

main()

关于python - line_profiler 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436899/

相关文章:

python - pygame.error : video system not initialized. pygame.init() 已经调用

python - 如何限制到套接字的连接数并在客户端触发超时(Python)

java - 分析 Java Web Start 应用程序

尝试分析简单程序时出现 Python-Hotshot 错误

linux - 使用 perf 根据其他事件的发生来获取事件计数

python - 我该如何替换 python 中的正则表达式?

python - 当使用@click.option 将命令行参数传递给函数时,如何返回值?

python - 使用 lxml,找到所有 td 类 ='banana',但前提是它们包含文本 "Today"

c++ - 以纳秒为单位测量精确时间 C++

c++ - 哪个是 KProf 的最佳替代品?