![]() |
|
[Tut] How to Measure Elapsed Time in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Measure Elapsed Time in Python? (/thread-99340.html) |
[Tut] How to Measure Elapsed Time in Python? - xSicKxBot - 05-05-2022 How to Measure Elapsed Time in Python? Summary: You can evaluate the execution time of your code by saving the timestamps using Code: time.time()Table of Contents
Problem: Given a Python program; how will you measure the elapsed time ( the time taken by the code to complete execution)? Consider the following snippet: Code: import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x p = perimeter(8)
print("Perimeter: ", p)
a = area(8)
print("Area: ", a)
Tidbit: Code: sleep()Code: timeNow, let us conquer the given problem and dive into the solutions. [b]Method 1: Using time.time()[/b] Code: time.time()Code: timeThe code: Code: import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = time.time() start = time.time()
p = perimeter(8)
end = time.time()
print("Perimeter: ", p)
print("Time Taken by perimeter(): ", end - start) start = time.time()
a = area(8)
end = time.time()
print("Area: ", a)
print("Time Taken by area(): ", end - start) end = time.time()
print("Total time elapsed: ", end - begin)Output: Code: Perimeter: 32
Time Taken by Perimeter(): 5.0040647983551025
Area: 64
Time Taken by area(): 2.0023691654205322
Total time elapsed: 7.006433963775635Approach: ➤ Keep track of the time taken by each function by saving the time stamp at the beginning of each function with the help of a start variable and using the Code: time()➤ Similarly, the end time, i.e., the timestamp at which a function completes its execution, is also tracked with the help of the Code: time()➤ Finally, the difference between the end and the start time gives the total time taken by a particular function to execute. ➤ To find the total time taken by the entire program to complete its execution, you can follow a similar approach by saving the time stamp at the beginning of the program and the time stamp at the end of the program and then find their difference. Discussion: If you are working on Python 3.3 or above, then another option to measure the elapsed time is Code: perf_counterCode: process_timeCode: time.clock[b]Method 2: Using time.perf_counter()[/b] In Python, the Code: perf_counter()Code: import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = time.perf_counter() start = time.perf_counter()
p = perimeter(8)
end = time.perf_counter()
print("Perimeter: ", p)
print("Time Taken by perimeter(): ", end - start) start = time.perf_counter()
a = area(8)
end = time.perf_counter()
print("Area: ", a)
print("Time Taken by area(): ", end - start) end = time.perf_counter()
print("Total time elapsed: ", end - begin)Output: Code: Perimeter: 32
Time Taken by perimeter(): 5.0133558
Area: 64
Time Taken by are(): 2.0052768
Total time elapsed: 7.0189293Caution: The Code: perf_counter()Code: perf_counterCode: perf_counter()[b]Method 3: Using time.process_time()[/b] Another method from the time module used to estimate the execution time of the program is Code: process_time()Code: process_time()Code: import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = time.process_time() start = time.process_time()
p = perimeter(8)
end = time.process_time()
print("Perimeter: ", p)
print("Time Taken by perimeter(): ", end - start) start = time.process_time()
a = area(8)
end = time.process_time()
print("Area: ", a)
print("Time Taken by area(): ", end - start) end = time.process_time()
print("Total time elapsed: ", end - begin)Output: Code: Perimeter: 32
Time Taken by perimeter(): 5.141000000000173e-05
Area: 64
Time Taken by area(): 4.1780000000005146e-05
Total time elapsed: 0.00029919000000000473Code: timeitNote: The Code: timeitLet us dive into the different methods of this module to understand how you can use it to measure execution time within your code. [b]4.1 Using timeit.timeit()[/b] Example 1: In the following example, we will have a look at a lambda function being executed with the help of the timeit module such that we will be specifying the number of times this anonymous function will be executed and then calculate the time taken to execute it. Code: import timeit count = 1 def foo(x): global count print(f'Output for call{count} = {x * 3}') count += 1 a = timeit.timeit(lambda: foo(8), number=3)
print("Time Elapsed: ", a)Output: Code: Output for call1 = 24
Output for call2 = 24
Output for call3 = 24
Time Elapsed: 6.140000000000312e-05Explanation: After importing the Code: timeitCode: timeit.timeit()[b]4.2 [b]Using timeit.repeat[/b][/b] Even though the above method allowed us to calculate the execution time of a lambda function, it is not safe to say that the value evaluated by the Code: timeit()Code: timeit.repeat()Example: Code: import timeit count = 1 def foo(x): global count print(f'Output for call{count} = {x * 3}') count += 1 a = timeit.repeat(lambda: foo(8), number=1, repeat=3)
print(a)
s = 0
for i in a: s = s + i
print("Best Outcome: ", s)Output: Code: Output for call1 = 24
Output for call2 = 24
Output for call3 = 24
[5.160000000001275e-05, 1.3399999999996748e-05, 1.0399999999993748e-05]
Best Outcome: 7.540000000000324e-05Instead of using Code: timeit.timeit()Code: timeit.default_timer()Code: timeit.default_timer()Code: time.time()Example: Code: import timeit
import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = timeit.default_timer() start = timeit.default_timer()
p = perimeter(8)
end = timeit.default_timer()
print("Perimeter: ", p)
print("Time Taken by Perimeter(): ", end - start) start = timeit.default_timer()
a = area(8)
end = timeit.default_timer()
print("Area: ", a)
print("Time Taken by Perimeter(): ", end - start) end = timeit.default_timer()
print("Total time elapsed: ", end - begin)Output: Code: Perimeter: 32
Time Taken by Perimeter(): 5.0143883
Area: 64
Time Taken by Perimeter(): 2.0116591
Total time elapsed: 7.0264410999999996The elapsed time can also be calculated using the Code: DateTime.datetime.now()Code: timeit()Example: Code: import datetime
import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = datetime.datetime.now() start = datetime.datetime.now()
p = perimeter(8)
end = datetime.datetime.now()
print("Perimeter: ", p)
print("Time Taken by Perimeter(): ", end - start) start = datetime.datetime.now()
a = area(8)
end = datetime.datetime.now()
print("Area: ", a)
print("Time Taken by Perimeter(): ", end - start) end = datetime.datetime.now()
print("Total time elapsed: ", end - begin)Output: Code: Perimeter: 32
Time Taken by Perimeter(): 0:00:05.003221
Area: 64
Time Taken by Perimeter(): 0:00:02.011262
Total time elapsed: 0:00:07.014483Thus to sum things up, you can use one of the following modules in Python to calculate the elapsed time of your code:
With that, we come to the end of this tutorial, and I hope you found it helpful. Please subscribe and stay tuned for more interesting articles. Here’s a list of highly recommended tutorials if you want to dive deep into the execution time of your code and much more:
|