1import time
2
3t0 = time.time()
4code_block
5t1 = time.time()
6
7total = t1-t0
1import time
2
3start = time.time()
4print("hello")
5end = time.time()
6print(end - start)
7
1import time
2seconds = time.time()
3print("Seconds since epoch =", seconds)
4
1from time import sleep, time
2
3start = time()
4sleep(2)
5print(format(time() - start, '.3f'), 's', sep='') # 2.003s
1import time
2thistime = time.time()
3# Here's an idea!
4def CountTime():
5 while(True):
6 time.sleep(1)
7 print(thistime)
8CountTime()
1Proper answer to timing a loop over a function multiple times
2import timeit
3timeit.timeit('func_to_time()',globals=globals(),number=1000)