# SOLVED: Measure Execution Time in Python [5 Methods]

Written By - Azka Iftikhar

## Introduction

Measuring python time start time stop is very important, since in many algorithms, time needs to be calculated and compared and the fastest one is usually selected. There is no function or library in python that directly so in this article we will see how we can achieve the execution time using different techniques. ## Method-1 : Use the python time library

One way to find python  time start time  stop  is to use the time library declare two variables and subtract the start time from end time, this will be the final time.

Code is :

``````import time
import random

start=time.time()

for i in range(1,1000000):
a=random.randint(1,1000000)
b=random.randint(1,1000000)
c=a+b
end=time.time()

print("the time is " + str(end-start)+ " seconds")``````

Output of this code is :

`the time is 2.265195369720459 seconds`

## Method-2 : Use sleep timer to do complex computations

If you are doing complex computation, you can use the sleep function to make the main thread sleep, in the meantime the computation can be done and after that , the execution time will be the same as the sleep time , code for this is

``````import time

def complexCompute(n):
for i in range(n):
for j in range(n):
for k in range(n):
pass

def main():
for i in range(10):
complexCompute(10)
time.sleep(0.02)
print("code time is {}".format(time.time()))

if __name__ == '__main__':
main()``````

Output of this code is :

```code time is 1660055052.451363
code time is 1660055052.4840262
code time is 1660055052.5157845
code time is 1660055052.5469954
code time is 1660055052.5787077
code time is 1660055052.6094527
code time is 1660055052.6407127
code time is 1660055052.6728058
code time is 1660055052.703756
code time is 1660055052.7342584```

While using `time.time()`, keep in mind that the time is shown in floating point numbers, which may be confusing at times.

## Method-3 : Use time.time_ns() to get rid of rounding off errors

The logic behind the code is similar to the first way that we studied, but to get accurate and more readable results, we use time_ns(). If I write the same code as above but use time_ns() compare the outputs.

Code :

``````# python  time start time  stop
import time
import random

# python  time start
start=time.time_ns()

for i in range(1,1000000):
a=random.randint(1,1000000)
b=random.randint(1,1000000)
c=a+b
end=time.time_ns()

# print python time start time  stop
print("the time taken for script execution is " + str(end-start)+ " nano seconds")``````

The output of this code is :

`the time taken for script execution is 2321635400 nano seconds`

## Method-4 : CPU execution time

CPU execution time can be used to calculate the python time start time stop . the code is following

import time

``````# python time start time stop
start_time_ns = time.process_time_ns()

# random computation
end_time_ns = time.process_time_ns()

print ("Time taken for script execution:", end_time_ns - start_time_ns)``````

## Method-5 : Using datetime module

We can also use python datetime module to get the script execution time:

``````import time
import random

from datetime import timedelta
start_time = time.monotonic()
for i in range(1,1000000):
a=random.randint(1,1000000)
b=random.randint(1,1000000)
c=a+b
end_time = time.monotonic()

# Calculate execution time
print(timedelta(seconds=end_time - start_time))``````

Output:

`0:00:01.785409`

## What is the best way?

To compare all of the ways to calculate execution time, almost all of them are almost similar but in some cases, for example if we make the main thread sleep for some time, and the computation completes before that time, the execution time that is extended is useless. In the `time.time.now()` method, it is not easy to read time in floating points, thus the nano-seconds technique can be best to calculate python time start stop time. Although in some cases the sleep method can be helpful, when complex computations are long and need more time for execution.

## Conclusion

There are different ways to calculate the python time start time stop we studied four ways, one is to measure the start time and then the stop time and subtracting both, second is using sleep method so that main thread wait for the complex computations. Other than that, cpu execution time or time in nano seconds can be used to calculate python time start time stop.

Related Keywords: python timeit, python time function runtime, python execution time in minutes, python time elapsed, python measure execution time of code block, python time sleep, python calculate execution time in seconds, python execution time in milliseconds, python measure execution time, running time python, python running time, python time execution, python timing execution, python execution time

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.