5.3 Profiling and Optimization
Profiling and optimization help improve the speed and efficiency of a program. Profiling measures how much time and resources each part of the code uses. Optimization focuses on making slow or resource-heavy parts faster. These techniques are important for building high-performance applications. Regular profiling ensures that code remains efficient as it grows.
DO YOU KNOW?
lightbulbProfiling helps identify slow parts of a program so they can be improved for better performance.
Example:
The following code shows a nested loop:
for i in range(10000):
for j in range(10000):
# Some repeated task
pass
In this code, the outer loop runs 10,000 times. For every single iteration of the outer loop, the inner loop also runs 10,000 times. Therefore, the total number of executions becomes: This means the inner statement may run one hundred million times, which can make the program very slow. Such a part of the code is called a performance bottleneck. Profiling tools help programmers identify these slow sections so that they can optimise the code and improve program efficiency.