python execution time function code example
Example 1: python time execution
import time
start_time = time.time()
print("Hello")
end_time = time.time()
total_time = end_time - start_time
print("Time: ", total_time)
Example 2: python - time of execution
import time
from datetime import timedelta
start_time = time.monotonic()
end_time = time.monotonic()
print(timedelta(seconds=end_time - start_time))
Example 3: how to time python code
from timer import Timer
t = Timer()
t.start()
t.stop()
Example 4: how to use timeit in python 3
import timeit
import_module = "import random"
testcode = '''
def test():
return random.randint(10, 100)
'''
print(timeit.repeat(stmt=testcode, setup=import_module))