How to measure execution time of small code snippets in Python

2 Answers

0 votes
import timeit

print(timeit.timeit('"*".join(str(n) for n in range(500))', number=10000))



'''
run:

1.272735002450645

'''

 



answered Mar 21, 2021 by avibootz
0 votes
import timeit

code='''
for i in range(1000): 
    result = sqrt(i)
'''

print(timeit.timeit(stmt=code, setup="from math import sqrt", number=10000))


 
 
'''
run:
 
1.6891817130381241

'''

 



answered Jul 26, 2022 by avibootz

Related questions

1 answer 313 views
2 answers 292 views
1 answer 257 views
1 answer 199 views
1 answer 201 views
3 answers 225 views
...