Created
May 10, 2020 07:13
-
-
Save jace/9897629abda9bbd06f5a1bf862f43d42 to your computer and use it in GitHub Desktop.
Performance difference of Python's partial vs partialmethod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import partial, partialmethod | |
from timeit import timeit | |
class Partial(Base): | |
def __init__(self): | |
self.p = partial(self.foo, 1) | |
class PartialMethod(Base): | |
p = partialmethod(Base.foo, 1) | |
# `partial` is significantly faster than `partialmethod` despite | |
# repeated instantiation (reconfirmed by reversing test order). | |
print(timeit('p = Partial(); p.p()', globals=globals())) | |
print(timeit('p = PartialMethod(); p.p()', globals=globals())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment