How is Your Code Doing ?
Majority of software projects does performance analysis at the final stage , when we have the application ready. Developers and managers tend to forget that performance should be taken as a feature development and not as an end result. Even at the end, we mostly focus on how application as whole is doing and troubleshooting measures might not even involve any code changes. I have experienced that even code changes could improve the performance of application in multiple folds . This blog is specific to performance analysis of Python code. There are various python as well as linux packages available to check the performance of your code . Here are couple of them which I found useful .
Linux utilities :
1. Perf - https://perf.wiki.kernel.org/index.php/Tutorial
perf stat python yourprogram.py
Performance counter stats for 'python myprogram.py':
9.526617 task-clock # 0.957 CPUs utilized
2 context-switches # 0.210 K/sec
0 CPU-migrations # 0.000 K/sec
988 page-faults # 0.104 M/sec
0.009957910 seconds time elapsed
2. time - http://www.lehman.cuny.edu/cgi-bin/man-cgi?time+1
$ time python yourprogram.py
real - Total time taken by your program to run ( start to finish)
user- The amount of CPU time spent in user-mode code (outside the kernel) within the process .
sys- refers to the amount of cpu time spent inside kernel specific functions
1. line_profiler - http://pythonhosted.org/line_profiler/
It is used for line by line profiling the python code.
2. memory_profiler - https://pypi.python.org/pypi/memory_profiler
Profiles Memory consumption of a process .
3. objgraph - https://pypi.python.org/pypi/objgraph
Figure out the memory leaks in your program