You are currently viewing Get Time of a Python Program Execution?

How do I get time of a Python program’s execution? Python provides modules and packages that make it easy to measure the execution time of a program. In this article, we will learn different methods for measuring the execution time of a Python program.

Advertisements

1. Quick Examples to Get Time of a Program Execution

These examples will give you a quick and high-level overview of how to measure the execution time of a program using the time and timeit modules. We will discuss each of these methods in more detail in the following sections.


# Import modules
import time
import timeit

# Method 1:Using the time module
start_time = time.time()

# Put code here
for i in range(1000000):
    pass

end_time = time.time()
elapsed_time = end_time - start_time
print(f"Execution time: {elapsed_time}")

# Method 2: Using the timeit module
execution_time = timeit.timeit('''
for i in range(1000000):
    pass
''', number=100)
print(f"Execution time: {execution_time} ")

2. Execution Time of a Python Program

The execution time of a Python program refers to the amount of time it takes for the program to run from start to finish. Measuring execution time can help you to identify parts of your code that are running slowly and might benefit from optimization.

To find the execution time of a program in Python follow these steps:

  1. Note the starting time before running the program.
  2. Run the program.
  3. Note the finishing time after the program has been completed.
  4. Calculate the execution time by subtracting the starting time from the finishing time.

Below are a few methods that can help you find the execution time of a Python Program:

3. Use time Module to Get Timing of Python Program Execution

We can use the time module in Python to find the execution time of the program. Though the time module has a lot of purposes, however, we can use it to find the execution time.

By following the general steps that we have mentioned earlier, we can not the starting time and then finish time. This was we can find the execution time. The module provides a function time() which returns the current time in seconds.

See the below sample template for finding the execution time of a program using the time module:


# Import
import time

# Record the start time
start_time = time.time()

# Run the program
# ...

# Record the end time
end_time = time.time()

# Calculate the execution time
exe_time = end_time - start_time

# Print the execution time
print("Execution time:", exe_time)

You can use this sample template for any program. Just put the code in the middle of the starting and finishing points and you will have the execution time.

See the below example:


import time

# Function to find the factorila
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
# Note the start time
start_time = time.time()
# Call the function
result = factorial(100)
# Call the finishing point
end_time = time.time()
exe_time = end_time - start_time

print(result)
print(exe_time)

4. timeit module – Python Program Execution Time

It is designed specifically for measuring the execution time of small code snippets. It provides a simple way to time the execution of code. The timeit module provides a Timer class. The Timer class runs the statement repeatedly and generate the reports the total time taken.

To use the timeit module, follow these steps:

  1. Import the timeit module.
  2. Create a Timer object with the code to be timed.
  3. Run the Timer object using the timeit() method.
  4. Get the execution time from the timeit() method.

See the following general template for using timeit module for finding the execution time.


# Import timeit module
import timeit

# Create a Timer object with the code to be timed and the setup code
timer = timeit.Timer(stmt='...', setup='...')

# Run the timer and get the execution time
execution_time = timer.timeit(number=1)

# Print the execution time to the console
print("Execution time:", execution_time)

Example using the timeit module:


import timeit

# Function for Testing
def factorial(n):
    if n <= 1:
        return 1
    else:
        return n * factorial(n-1)

# Measure the execution time of the factorial function
t = timeit.timeit(lambda: factorial(10), number=1000)

print("Execution time: ", t, "seconds")

5. Factors Affecting Execution Time

The program you execute on your computer may have a different execution time than you run on some other computer. The reason is that the execution time is different on different system. It is affected by different things. Following are some of the common factors that affect the execution time:

  1. The hardware on which the program is executed can significantly impact its execution time.
  2. Software environment in which the program is executed can also impact its execution time. The operating system, other running programs, and libraries used by the program can all affect its performance.
  3. The complexity of the code can also affect its execution time. Code that requires a lot of computational resources may take longer to execute.
  4. Size of the input data can also affect the execution time. Programs that operate on large data sets may take longer to execute than those that operate on smaller data sets.
  5. Input/output (I/O) operations can also impact the execution time of a program.

6. Summary and Conclusion

In this article, you have learned to measure the execution time of a Python program with examples. We discussed three different methods for measuring execution time. I hope this article was helpful. If you have any questions, please leave them in the comment section.

Happy Coding!

Related Article