rkern / line_profiler

(OLD REPO) Line-by-line profiling for Python - Current repo ->
https://github.com/pyutils/line_profiler
Other
3.6k stars 254 forks source link

How to avoid commenting and uncomment @profile #121

Closed shenkev closed 5 years ago

shenkev commented 6 years ago

Currently I'm just commenting out @profile whenever I'm running the code for production and uncommenting @profile when I need to profile. Is there a better way? I don't like explicitly using the LineProfiler class which I believe requires you to pass in the function arguments as well. I want to profile functions as I run them usually.

SyamGadde commented 6 years ago

I always insert at the top of my code:

try:
  dummy = profile
except:
  profile = lambda x: x

That way the profiling decorator is a no-ops if I don't run it under kernprof. The only performance penalty, if at all, is at load time.