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 410 views
1 answer 247 views
247 views asked Sep 26, 2021 by avibootz
2 answers 285 views
1 answer 419 views
2 answers 434 views
1 answer 370 views
1 answer 301 views
...