Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 916
Paul Avatar asked Oct 22 '25 09:10

Paul


1 Answers

Try add

profiler.enable_by_count()

before

main()

like image 58
e39a562r Avatar answered Oct 23 '25 23:10

e39a562r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!