0

I would like to know if there is anyway to profile only a function in a python script which does not have a main(). I have a script which will be called by another one. But i want to know the loopholes in it.

I first use the commmand kernprof -l -v filename.py

and then python -m line_profiler filename.py.lprof

I have added @profile before the function.

Anonymous
  • 86
  • 1
  • 7
  • There's several ways of profiling your code. I like [line_profiler](https://github.com/rkern/line_profiler) as it gives detail for individual lines rather than the function as a whole. Then you just decorate the function you want to profile with `@profile`. – roganjosh Feb 14 '17 at 12:21
  • Yes i tried line_profiler but i am not getting the output for every line. Also does it work for scripts without main()? – Anonymous Feb 14 '17 at 12:22
  • It's always given line-by-line for me in any function I have decorated. In regards to the `main()` I'm not sure what you're asking; I guess yes? – roganjosh Feb 14 '17 at 12:24
  • Thanks. I am able to get the output only for the first 2 lines and then its a blank. – Anonymous Feb 14 '17 at 12:27
  • In that case it might be better to edit this question with the specific input function and how you're using `line_profiler` plus the data contained in the file it creates. Currently this is a very broad question. – roganjosh Feb 14 '17 at 12:28
  • Your edit doesn't include all of the information I suggested. Try `python kernprof.py -l -v my_script_name.py > profiler_results.txt` to redirect the output to a file. – roganjosh Feb 14 '17 at 12:36
  • You might take a look at [*this*](http://stackoverflow.com/a/4299378/23771). – Mike Dunlavey Feb 14 '17 at 14:26

0 Answers0