Why line_profiler enable and disable output only 'Timer unit'?

459 Views Asked by At

enviroment

  • python 3.9
  • line-profiler 3.1.0

I installed line-profiler by pip.

sudo pip3 install line-profiler

detail

add_function and runcall works. that's ok.

import line_profiler

def hoge():
    n = 0
    for i in range(100):
        n += i
    print(n)

pr = line_profiler.LineProfiler()
pr.add_function(hoge)
pr.runcall(hoge)
pr.print_stats()
% python3.9 profile1.py
4950
Timer unit: 1e-06 s

Total time: 6.1e-05 s
File: /home/miwa/work/lang/python/profile1.py
Function: hoge at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def hoge():
     5         1          1.0      1.0      1.6      n = 0
     6       101         23.0      0.2     37.7      for i in range(100):
     7       100         25.0      0.2     41.0          n += i
     8         1         12.0     12.0     19.7      print(n)

but enable and disable does not work.

Output only 'Timer unit' .

Is it used incorrectly?

import line_profiler

def hoge():
    n = 0
    for i in range(100):
        n += i
    print(n)

pr = line_profiler.LineProfiler()
pr.enable()
hoge()
pr.disable()
pr.print_stats()
% python3.9 profile0.py
4950
Timer unit: 1e-06 s
1

There are 1 best solutions below

0
On

It seems line-profiler cannot be used in the same way as cProfile (enable - run - disable syntax). Have a look at this answer for two alternative ways.