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 268 views
1 answer 153 views
153 views asked Sep 26, 2021 by avibootz
2 answers 155 views
1 answer 313 views
2 answers 292 views
1 answer 257 views
1 answer 223 views
...