Package: auto-profiler
auto_profilerA realtime timer for profiling a Python function or snippet.FeaturesFiltering external libraries profiling.Filtering very short time functions-> thresholdAllow depth: you can easily find the time consuming functionAllow loop or multiple function callAllow recursive function callDisable it globaly by Profiler.GlobalDisable=True to save time :)InstallationRelease version:$pipinstallauto_profilerDevelopment version:$pipinstall-egit+https://github.com/modaresimr/auto_profiler.git#egg=auto_profilerInstall in Jupyter$pipinstallipytree
$jupyternbextensionenable--py--sys-prefixipytreeQuick startJupyter NotebookAuto profilingMore commonly, chances are that we want to measure the execution time of an entry function and all its subfunctions. In this case, it's too tedious to do it manually, and we can leverageProfilerto inject all the timing points for us automatically:importtime# line number 1importrandomfromauto_profilerimportProfiler,Treedeff1():mysleep(.6+random.random())defmysleep(t):time.sleep(t)deffact(i):f1()if(i==1):return1returni*fact(i-1)@Profiler()defmain():foriinrange(5):f1()fact(3)if__name__=='__main__':main()Example OutputIn JupyterTime   [Hits * PerHit] Function name [Called from] [function location]
-----------------------------------------------------------------------
8.974s [1 * 8.974]  main  [auto-profiler/profiler.py:267]  [/test/t2.py:30]
├── 5.954s [5 * 1.191]  f1  [/test/t2.py:34]  [/test/t2.py:14]
│   └── 5.954s [5 * 1.191]  mysleep  [/test/t2.py:15]  [/test/t2.py:17]
│       └── 5.954s [5 * 1.191]  <time.sleep>
|
|
|   # The rest is for the example recursive function call fact
└── 3.020s [1 * 3.020]  fact  [/test/t2.py:36]  [/test/t2.py:20]
    ├── 0.849s [1 * 0.849]  f1  [/test/t2.py:21]  [/test/t2.py:14]
    │   └── 0.849s [1 * 0.849]  mysleep  [/test/t2.py:15]  [/test/t2.py:17]
    │       └── 0.849s [1 * 0.849]  <time.sleep>
    └── 2.171s [1 * 2.171]  fact  [/test/t2.py:24]  [/test/t2.py:20]
        ├── 1.552s [1 * 1.552]  f1  [/test/t2.py:21]  [/test/t2.py:14]
        │   └── 1.552s [1 * 1.552]  mysleep  [/test/t2.py:15]  [/test/t2.py:17]
        └── 0.619s [1 * 0.619]  fact  [/test/t2.py:24]  [/test/t2.py:20]
            └── 0.619s [1 * 0.619]  f1  [/test/t2.py:21]  [/test/t2.py:14]Manual profilingSometimes, we only want to measure the execution time of partial snippets or a few functions, then we can inject all timing points into our code manually by leveragingTimer:# manual_example.pyimporttimefromauto_profilerimportTimer,Treedefmain():t=Timer('sleep1',parent_name='main').start()time.sleep(1)t.stop()t=Timer('sleep2',parent_name='main').start()time.sleep(1.5)t.stop()print(Tree(Timer.root))if__name__=='__main__':main()Run the example code:$pythonmanual_example.pyand it will show you the profiling result:2.503s  main
├── 1.001s  sleep1
└── 1.501s  sleep2advanced setupdef show(p):
    print('Time   [Hits * PerHit] Function name [Called from] [Function Location]\n'+\
          '-----------------------------------------------------------------------')
    print(Tree(p.root, threshold=0.5))
    
@Profiler(depth=4, on_disable=show)
def main():
    for i in range(5):
        f1()

    fact(3)Supported frameworksWhile you can do profiling on normal Python code, as a web developer, chances are that you will usually do profiling on web service code.Currently supported web frameworks:FlaskExamplesFor profiling web service code (involving web requests), check outexamples.LicenseMIT