Code Timing

PHOTO EMBED

Thu Mar 04 2021 08:21:23 GMT+0000 (Coordinated Universal Time)

Saved by @Bartok #python

import time,timeit


def power(limit):
    return [x**2 for x in range(limit)]




def measure_runtime(func):
    start = time.time()
    func()
    end = time.time()
    print(end - start) # number of seconds since 1970



measure_runtime(lambda :power(5000000)) # lamda function allows us to pass an argument


## another way


print(timeit.timeit('[x**2 for x in range(10)]')) # it runs it many times many times
print(timeit.timeit('list(map(lambda x: x**2,range(10)))'))
content_copyCOPY