How to Minimize Python Script Execution Time?
Last Updated :
18 Mar, 2024
We have a problem statement of how to minimize script execution time in Python. Minimizing the Python Script execution becomes very crucial for getting better and optimized output for an application in production or when we are doing CP or DSA. In this article, we will see how to minimize script execution time using different methods and approaches.
Minimize Python Script Execution Time
Below are some of the ways to minimize script execution time in Python:
Minimize Python Script Execution Time Using List
In this example, the below code compares the execution time of an inefficient approach using a list for membership testing against an efficient approach using a set. It measures the time taken for each method using the `time it` module. The output demonstrates that the set approach is more time-efficient, emphasizing the impact of data structure choice on script execution time.
Python3
import timeit
# approach using set
start_time = timeit.default_timer()
my_set = set(range(1, 1000000))
element_to_find = 999999
result = element_to_find in my_set
end_time = timeit.default_timer()
print("Time taken (inefficient):", end_time - start_time)
# Efficient approach
start_time = timeit.default_timer()
my_list = list(range(1, 1000000))
element_to_find = 999999
result = element_to_find in my_list
end_time = timeit.default_timer()
print("Time taken (efficient):", end_time - start_time)
OutputTime taken (inefficient): 0.11602964600024279
Time taken (efficient): 0.09393205099877378
Minimize Python Script Execution Time Using Memoization
In this example, below code compares the execution time of an inefficient loop, where a list is populated by multiplying each element by 2 within a traditional loop, against an efficient approach using list comprehension. The `timeit` module measures the time taken for each method. The output showcases the time efficiency of list comprehension, emphasizing its concise syntax and optimized performance compared to the traditional loop.
Python3
import timeit
# Inefficient loop
start_time = timeit.default_timer()
my_list = []
for i in range(1, 1000000):
my_list.append(i * 2)
end_time = timeit.default_timer()
print("Time taken (inefficient loop):", end_time - start_time)
# Efficient list comprehension
start_time = timeit.default_timer()
my_list = [i * 2 for i in range(1, 1000000)]
end_time = timeit.default_timer()
print("Time taken (efficient list comprehension):", end_time - start_time)
OutputTime taken (inefficient loop): 0.25805007700182614
Time taken (efficient list comprehension): 0.10241214800043963
Membership Test Optimization
In this example, below code evaluates the execution time of an inefficient recursive Fibonacci Function against an efficient memoized version. The `timeit` module measures the time taken for each method. The output highlights the time efficiency gained by memoization, where previously computed results are cached.
Python3
import timeit
# Inefficient memoized Fibonacci function
memo = {}
def fib_memoized(n):
if n <= 1:
return n
if n not in memo:
memo[n] = fib_memoized(n - 1) + fib_memoized(n - 2)
return memo[n]
start_time = timeit.default_timer()
result = fib_memoized(30)
end_time = timeit.default_timer()
print("Time taken (Inefficient memoized):", end_time - start_time)
# Efficient recursive Fibonacci function
def fib_recursive(n):
if n <= 1:
return n
return fib_recursive(n - 1) + fib_recursive(n - 2)
start_time = timeit.default_timer()
result = fib_recursive(30)
end_time = timeit.default_timer()
print("Time taken (Efficient recursive):", end_time - start_time)
OutputTime taken (Inefficient memoized): 2.915099867095705e-05
Time taken (Efficient recursive): 0.5552801470003033
Share your thoughts in the comments
Please Login to comment...