Code Profiling
The main aspect while writing any code, especially when deploying, is that it should consume the lowest computational time and cost.
Recently, it was assigned to me to improve the performance of a piece of code. To do it I have discovered a new concept which is profiling.
Today I want to talk about this important concept in coding that improves the performance of your code and that makes your application way better.
Profiling responded to all these questions:
- When to optimize?
- How do identify performance problems?
- How many times is each method in my code gets called?
- How long does each of these methods take?
- How to reduce runtime?
Overview:
- Introduction
- Code Profiling Benefits
- Python Code Profiling Test
- References
Introduction:
According to Wikipedia:
In software engineering, profiling (“program profiling”, “software profiling”) is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls. Most commonly, profiling information serves to aid program optimization, and more specifically, performance engineering.
Simply put, code profiling is a method that is used to detect how long each function or line of code takes to run and how often it gets executed. This is an essential step toward finding bottlenecks in your code and therefore understanding how your code can be optimized. Our method is quantitative in nature as it produces summary statistics on individual lines of code. As a result, it provides actionable insights into which parts of your code require further optimization.
Code Profiling Benefits:
let’s list some of the Benefits that code profiling provides to developers and testers:
- It makes software development cycles shorter and more agile.
- It keeps the application performing reliably under all circumstances.
- It improves the end-user experience by allowing developers to fix anomalies in real-time.
- It figures out what is using a huge percent of the total CPU usage of your code
- It finds memory leaks early and finds the hot path in your code
Python Code Profiling Test:
I use an old code that I wrote in the HackerRank challenge and I use two solutions and compare results by code profiling.
The problem: is to create a function check password that has a minimum length is at least 6 and has at least (one digit, one lowercase English character, one uppercase English character, and one special character”!@#$%^&*()-+”)
Steps:
1. Create a profiling decorator function:
2. Apply to the solution function with @profile():
3. use cProfile to create a report:
python3 -m cProfile -o min_password_test1.profile min_password.py
4. install snakeviz method used to visualize the report :
pip install snakeviz
5. show the report by using:
snakeviz min_password_test1.profile
Then, I do the same with the second solution
Finlay compares the results:
From the two reports, I discover that the first solution spends less runtime than the second solution
The first solution made it in (0.000870s)
The second solution made it in (0.000799s)
In summary, it seems that it’s not a big difference for my test code but in a big program it makes a huge difference to choose the better code performance.
Also for the visualization, there are a lot of other tools but I wanna share a new tool I used and a new concept that I learned and found very useful.
The last thing, Thank you for getting this far, I hope you found this blog post helpful.