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.

Revisions

  1. AO8 created this gist Nov 8, 2019.
    27 changes: 27 additions & 0 deletions profiler.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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