import random
import timeit
TAX_RATE = 0.08
PRICES = [random.randrange(100) for _ in range(100_000)]
def get_price(price):
return price * (1 + TAX_RATE)
def get_prices_with_map():
return list(map(get_price, PRICES))
def get_prices_with_comprehension():
return [get_price(price) for price in PRICES]
def get_prices_with_loop():
prices = []
for price in PRICES:
prices.append(get_price(price))
return prices
ntimes=50
print('map version :', timeit.timeit(get_prices_with_map, number=ntimes))
print('comprehension version:', timeit.timeit(get_prices_with_comprehension, number=ntimes))
print('for loop version :', timeit.timeit(get_prices_with_loop, number=ntimes))
To embed this project on your website, copy the following code and paste it into your website's HTML: