1import time
2
3start = time.time()
4print("hello")
5end = time.time()
6print(end - start)
7
1import time
2
3# Calculate the power of two for a defined range of number
4def power_two(my_range):
5 return [x**2 for x in range(my_range)]
6
7# Measure time
8def measure_time(func):
9 start = time.time()
10 func() # any specific function to measure
11 end = time.time()
12 print(end - start)
13
14measure_time(lambda: power_two(10000000)) # lambda permits to pass the argument of our function
1import time
2seconds = time.time()
3print("Seconds since epoch =", seconds)
4
1import time
2timer_length = float(input("How many seconds would you like you're timer to be set for? "))
3time.sleep(timer_length)
4print("Done!")
1from time import sleep, time
2
3start = time()
4sleep(2)
5print(format(time() - start, '.3f'), 's', sep='') # 2.003s
1Proper answer to timing a loop over a function multiple times
2import timeit
3timeit.timeit('func_to_time()',globals=globals(),number=1000)