How to calculate the execution time of part of a code in Python

3 Answers

0 votes
import time

start = time.time()

print("time used to execute this print: ")

end = time.time()

print(end - start)






'''
run:

time used to execute this print: 
7.152557373046875e-06

'''

 



answered Apr 30, 2021 by avibootz
0 votes
import time

start = time.perf_counter()

print("time used to execute this print: ")

end = time.perf_counter()

print(end - start)






'''
run:

time used to execute this print: 
7.610302418470383e-06

'''

 



answered Apr 30, 2021 by avibootz
0 votes
import time

start = time.process_time()

print("time used to execute this print: ")

end = time.process_time()

print(end - start)






'''
run:

time used to execute this print: 
1.0453000000000545e-05

'''

 



answered Apr 30, 2021 by avibootz

Related questions

2 answers 408 views
1 answer 246 views
246 views asked Sep 26, 2021 by avibootz
2 answers 284 views
1 answer 418 views
2 answers 432 views
1 answer 370 views
1 answer 301 views
...