Python profiling
Sometimes, you want your Python (Python 3, obviously) scripts to be efficient (and yes, it is possible!). To achieve good performance, you need a tool to evaluate it.
Ipython console
Before we go any further, let me introduce you to the IPython console (you can evaluate your code's performance without, but it will be far less convenient). Let me convince you to use the IPython console (ipython3 command) instead of the default console (python3 command) for your python experimentations. (Note: I am talking about using an interactive python console. Your scripts should still be executed with a python3 my_script.py command)
The IPython console (Interactive Python) display features like:
- Simpler syntax:
run my_script.py arg1 exitinstead ofimport sys sys.argv = ['my_script.py', 'arg1'] # trick to use arguments with open('my_script.py') as file: exec(file.read()) exit()
- Smarter auto-completion (on module names, and paths).
- Convenient help and function definition display (e.g.
time.time?ornp.dot?instead ofhelp(time.time)ornp.info(np.dot)) - Better history manipulation (especially with multiline commands), and editing shortcuts (Ctrl-r, Ctrl-Up, automatic identation, ...).
- Basic bash command support (
ls,cd,rm, ... within the Python console!) - Last but not least: magic commands!
ipython3 command in your terminal, or install it (sudo apt-get install ipython3) and thank me later!
Advice for intermediate users: use the Ctrl-o shortcut for enabling multiline editing, or use Ctrl-q Ctrl-j to add the first newline. It comes pretty handy in some cases, especially for timing measures...
Basic timing
It is time to try one of those magic commands: %timeit. With this, timing is a easy as:
Line by line profiling
Basic timings are fine for simple experimentations, but when dealing with scripts or non-trivial functions, one needs a more precise tool. Here is when profiling comes in. Profiling a script/function is basically timing each line of your program. And with the line_profiler module, it is quite convenient!
First let us install the module (if you do not have root access on your machine, simply add the --user option to your pip3 commands: pip3 install --user line_profiler).
Now, the profiling is as easy as:
Let us see it in action with a complete test:
Memory usage profiling
If you want to monitor/profile your memory usage, just use the memory_profiler module, and the associated %mprun magic command: