Calculating Execution Time of a Python Program
The execution or running time of a program indicates how quickly it delivers output based on the algorithm used to solve the problem.
![]() |
Execution Time Metrics |
Calculating the execution time is crucial, especially in large projects where efficiency is paramount.
To calculate the execution time of a Python program, follow these steps:
- Capture the start time of the program using a variable.
- Write your Python program.
- Record the end time of the program using another variable.
- Compute the difference between the start and end times to get the execution time in seconds.
Let's apply this process to measure the time taken by a Python program. Below is a simple example that creates acronyms:
from time import time
start_time = time()
# Python program to create acronyms
word = "Artificial Intelligence"
words = word.split()
acronym = ""
for w in words:
acronym += w[0].upper()
print("Acronym:", acronym)
end_time = time()
execution_time = end_time - start_time
print("Execution Time:", execution_time)
In the output, you will see the result of the Python program followed by its running time in seconds. This method helps in evaluating the efficiency of different approaches and optimizing performance in your projects.