Friday, September 14, 2018

kids these days think data structures grow on trees

Args and kwargs are great features of Python.  There is a measurable (though highly variable) cost of them however:

>>> timeit.timeit(lambda: (lambda a, b: None)(1, b=2))

>>> timeit.timeit(lambda: (lambda *a, **kw: None)(1, b=2))

>>> timeit.timeit(lambda: (lambda *a, **kw: None)(1, b=2)) - timeit.timeit(lambda: (lambda a, b: None)(1, b=2))

Constructing that dict and tuple doesn't happen for free:

>>> timeit.timeit(lambda: ((1,), {'b': 2})) - timeit.timeit(lambda: None)

Specifically, it takes about 1/5,000,000th of a second.

1 comment:

  1. I read this article, it is really informative one. Your way of writing and making things clear is very impressible. Thanking you for such an informative article. Data Structures Learning Online Videos