Yixuan-Wang / blog-contents

The storage of Pak blog's contents.
https://yixuan-wang.github.io/blog/
0 stars 0 forks source link

py-bench #28

Open Yixuan-Wang opened 3 weeks ago

Yixuan-Wang commented 3 weeks ago

title: Life is Long, Use Python date: 2024-11-03T16:30:00-06:00 category: comp tags:

Random Python benchmarks. Is Python fast yet 🐍?

Yixuan-Wang commented 3 weeks ago

Keyword Args of Partial Functions

[!tldr] Binding keyword arguments in functools.partial is slower than raw calls.

def f(a1, a2, a3, a4, a5, a6, a7):
    return sum([a1, a2, a3, a4, a5, a6, a7])

# Raw
f(1, a2=2, a3=3, a4=4, a5=5, a6=6, a7=7)

# Partial
f_partial = functools.partial(f, a2=2, a3=3, a4=4, a5=5, a6=6, a7=7)
f_partial(1)

# Spread
kwargs = {"a2": 2, "a3": 3, "a4": 4, "a5": 5, "a6": 6, "a7": 7}
f(1, **kwargs)
Impl Time (s)
Python (Raw) 0.135
Python (Partial) 0.237
Python (Spread) 0.213

In Python, keyword arguments **kwargs are potentially mutable within the function body.

def why_you_have_to_do_this(a, **kwargs):
    kwargs["a"] += a

Therefore, each call to the function has to create a new dict to store all keyword arguments. This copy also happens when using functools.partial. There is no performance gain compared to simply spreading a dict of arguments in the call, yet you have to pay for the overhead of merging arguments when using partial.