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)))'))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter