The first homework has already, worryingly, set a trend to (needlessly) micro-optimise the simplest of statements. In a bid to dissect and extinguish the lamest parts of my scripts I've resorted to using a line profiler, which describes the number of calls and execution time of every line of code. The common cProfiler instead presents aggregate information about function calls.
The "Software and Commands" page mentions a line profiler that works within Spyder, although I had multiple issues with it. For starters I was prompted to update or install 239**34 other required packages, change my family name and convert to Buddhism. It doesn't seem to be possible to run Spyder's line profiler outside of the IDE (Shift-F10) and without selecting each script to be profiled from the GUI. Now, I wouldn't touch Anaconda's stock IDE with a barge pole, so I find the command line based kernprof line profiler more convenient.
The steps to set it up were the following:
-
conda install -c anaconda line_profiler in "Anaconda's Prompt"
-
decorate the functions to be profiled with @profile
-
run kernprof -l -v script_name.py
One can also run "pytest" files with "kernprof", which will analyse all the decorated functions called by the tests. Unfortunately, running test files on their own, without relying on "kernprof" and while the decorator's enabled, throws an error as "pytest" complains that the "profile" module can't be found. I comment out the offending line when needed.
This is the kind of result one can expect. The line above the function's definition includes the decorator:
My gripes with the line profiler are two:
- it doesn't allow one to run multiple tests and average results (as with timeit, for instance) — one must manually set up multiple calls to decorated functions and look at aggregates
- pytest and both line profilers don't play well together whilst pytest's own profiler doesn't output execution statistics line by line
It'd be nice if anybody came up with ways to address either point.