Skip to content

Instantly share code, notes, and snippets.

@AO8
Created November 8, 2019 23:47
Show Gist options
  • Save AO8/28ca66b51ebb655f32dc19bd01c54fd3 to your computer and use it in GitHub Desktop.
Save AO8/28ca66b51ebb655f32dc19bd01c54fd3 to your computer and use it in GitHub Desktop.
A simple profiling example with the timeit module.
import random
import timeit
TAX_RATE = .08
txns = [random.randrange(100) for _ in range(10000000)]
def get_price(txn):
return txn * (1 + TAX_RATE)
def get_prices_with_map():
return list(map(get_price, txns))
def get_prices_with_comprehension():
return [get_price(txn) for txn in txns]
def get_prices_with_loop():
prices = []
for txn in txns:
prices.append(get_price(txn))
return prices
timeit.timeit(get_prices_with_map, number=10)
# 19.529933099999994
timeit.timeit(get_prices_with_comprehension, number=10)
# 21.178829399999984
timeit.timeit(get_prices_with_loop, number=10)
# 25.403499799999963
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment