Python provides many excellent modules to measure the statistics of a program. This makes us know where the program is spending too much time and what to do in order to optimize it. It is better to optimize the code in order to increase the efficiency of a program. So, perform some standard tests to ensure optimization and we can improve the program in order to increase efficiency. In this article, we will cover How do we profile a Python script to know where the program is spending too much time and what to do in order to optimize it.
Method 1: Python time module
Time in Python is easy to implement and it can be used anywhere in a program to measure the execution time. By using timers we can get the exact time and we can improve the program where it takes too long. The time module provides the methods in order to profile a program.
Example 1:
In this example, we are trying to calculate the time taken by the program to print a statement.
Python3
# importing time module
import time
start = time.time()
print("Time Consumed")
print("% s seconds" % (time.time() - start))
Output:
Time Consumed
0.01517796516418457 seconds
Example 2:
In this example, we are trying to calculate the time taken by the program to call a function and print the statement.
Python3
# importing time module
import time
def gfg():
start = time.time()
print("Time consumed")
end = time.time()
print("gfg() function takes", end-start, "seconds")
# Calling gfg
gfg()
Output:
Time consumed
gfg() function takes 0.015180110931396484 seconds
Method 2: Python line_profiler
Python provides a built-in module to measure execution time and the module name is LineProfiler.It gives a detailed report on the time consumed by a program.
Python3
# importing line_profiler module
from line_profiler import LineProfiler
def geek(rk):
print(rk)
rk = "geeks"
profile = LineProfiler(geek(rk))
profile.print_stats()
Output:
Timer unit: 4.27198e-10 s
Method 3: Python cProfile
Python includes a built-in module called cProfile which is used to measure the execution time of a program. The cProfiler module provides all information about how long the program is executing and how many times the function gets called in a program. The Python cprofile example:
Example 1:
Here are measuring the time that will take to calculate the following equation.
Python3
# importing cProfile
import cProfile
cProfile.run("10 + 10")
Output:
3 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 :1()
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Example 2:
The cProfile measures the statistics about any function.
Python3
# importing cProfile
import cProfile
def f():
print("hello")
cProfile.run('f()')
Output:
hello
5 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 3233da5f950795af777f4b63136f7efd.py:5(f)
1 0.000 0.000 0.000 0.000 :1()
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Example 3:
If more exact profiling control is required then Profile class is normally used over the cProfile.run() function provides.
Python3
# importing library
import cProfile
import pstats
import io
from pstats import SortKey
# Creating profile object
ob = cProfile.Profile()
ob.enable()
# As you increase the power time will increase
# as per your machine efficiency.
num = 18**200000
ob.disable()
sec = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(ob, stream=sec).sort_stats(sortby)
ps.print_stats()
print(sec.getvalue())
Output:
45 function calls in 0.044 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.000 0.000 0.044 0.022 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3396(run_code)
2 0.000 0.000 0.044 0.022 {built-in method builtins.exec}
1 0.044 0.044 0.044 0.044 <ipython-input-1-f7443c5d9495>:11(<module>)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\codeop.py:142(__call__)
2 0.000 0.000 0.000 0.000 {built-in method builtins.compile}
1 0.000 0.000 0.000 0.000 <ipython-input-1-f7443c5d9495>:13(<module>)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:238(helper)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:82(__init__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\traitlets\traitlets.py:564(__get__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:117(__exit__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:108(__enter__)
4 0.000 0.000 0.000 0.000 {built-in method builtins.next}
4 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\compilerop.py:166(extra_flags)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\hooks.py:103(__call__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\traitlets\traitlets.py:533(get)
4 0.000 0.000 0.000 0.000 {built-in method builtins.getattr}
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3334(compare)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\utils\ipstruct.py:125(__getattr__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:1278(user_global_ns)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\hooks.py:168(pre_run_code_hook)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Similar Reads
Pandas Profiling in Python
Pandas is a very vast library that offers many functions with the help of which we can understand our data. Pandas profiling provides a solution to this by generating comprehensive reports for datasets that have numerous features. These reports can be customized according to specific requirements. I
5 min read
Python print() function
The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
2 min read
Data profiling in Pandas using Python
Pandas is one of the most popular Python library mainly used for data manipulation and analysis. When we are working with large data, many times we need to perform Exploratory Data Analysis. We need to get the detailed description about different columns available and there relation, null check, dat
1 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
float() in Python
Python float() function is used to return a floating-point number from a number or a string representation of a numeric value. Example: Here is a simple example of the Python float() function which takes an integer as the parameter and returns its float value. Python3 # convert integer value to floa
3 min read
Precision Handling in Python
Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the "math" module. In this article, we will use high-precision calculations in Python with Decimal in Python.ExampleInput: x = 2.4Output: Integra
6 min read
Programming Paradigms in Python
Paradigm can also be termed as a method to solve some problems or do some tasks. A programming paradigm is an approach to solve the problem using some programming language or also we can say it is a method to solve a problem using tools and techniques that are available to us following some approach
4 min read
Functional Programming in Python
Functional programming is a programming paradigm in which we try to bind everything in a pure mathematical functions style. It is a declarative type of programming style. Its main focus is on " what to solve" in contrast to an imperative style where the main focus is "how to solve". It uses expressi
10 min read
dir() function in Python
The dir() function is a built-in Python tool used to list the attributes (like methods, variables, etc.) of an object. It helps inspect modules, classes, functions, and even user-defined objects during development and debugging.Syntaxdir([object])Parameters: object (optional): Any Python object (lik
3 min read
Python Module Index
Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read