1import time
2
3t0 = time.time()
4code_block
5t1 = time.time()
6
7total = t1-t0
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 timeit
2import_module = "import random"
3testcode = '''
4def test():
5 return random.randint(10, 100)
6'''
7print(timeit.repeat(stmt=testcode, setup=import_module))
8
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!")
1 1# latest_tutorial.py
2 2
3 3import time
4 4from reader import feed
5 5
6 6def main():
7 7 """Print the latest tutorial from Real Python"""
8 8 tic = time.perf_counter()
9 9 tutorial = feed.get_article(0)
1010 toc = time.perf_counter()
1111 print(f"Downloaded the tutorial in {toc - tic:0.4f} seconds")
1212
1313 print(tutorial)
1414
1515if __name__ == "__main__":
1616 main()
17