-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeit_test.py
More file actions
28 lines (22 loc) · 769 Bytes
/
Copy pathtimeit_test.py
File metadata and controls
28 lines (22 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import random
import timeit
TAX_RATE = .08
transactions = [random.randrange(100) for _ in range(100000)]
def get_price(transaction):
return transaction * (1 + TAX_RATE)
# map
def get_prices_with_map():
return list(map(get_price, transactions))
# comprehension
def get_prices_with_comprehension():
return [get_price(transaction) for transaction in transactions]
# loop
def get_prices_with_loop():
prices = []
for transaction in transactions:
prices.append(get_price(transaction))
return prices
# time it
print("Map: ", timeit.timeit(get_prices_with_map, number=100))
print("Comprehension: ", timeit.timeit(get_prices_with_comprehension, number=100))
print("Loop: ", timeit.timeit(get_prices_with_loop, number=100))