Paul
Paul

Reputation: 43620

line_profiler not working as expected

Trying to use line_profiler as an API. Following their docs and this tutorial (scroll down to Line Profiling), I get a minimalist test case for profiling some numpy ufuncs:

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()

I get this in stdout which indicates that main ran, but was not profiled:

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

I'm new to line_profiler. See my other q if curious why I don't use cProfile.

Upvotes: 3

Views: 2864

Answers (1)

e39a562r
e39a562r

Reputation: 85

Try add

profiler.enable_by_count()

before

main()

Upvotes: 3

Related Questions