how to get runtime in python code example

Example 1: find time of run for python code

import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

Example 2: how to get runtime of a function in python

import datetime
t_set = lambda: datetime.datetime.now().astimezone().replace(microsecond=0)
t_diff = lambda t: str(t_set() - t)
t_stamp = lambda t=None: str(t) if t else str(t_set())

Example 3: check runtime python

import timeit

code = """[4, 2, 3, 1, 5].sort()"""

execution_time = timeit.timeit(code, number=1)

print(execution_time)

Tags:

Misc Example