Overhead of creating classes in Python: Exact same code using class twice as slow as native DS?

后端 未结 2 1825
滥情空心
滥情空心 2021-02-08 20:41

I created a Stack class as an exercise in Python, using all list functions. For example, Stack.push() is just list.append(), Stack.pop() is list.pop() and Stack.isEmpty() is jus

2条回答
  •  情深已故
    2021-02-08 21:30

    There is an inherent overhead using functions (where methods on an instance are just wrappers around functions to pass in self).

    A function call requires the current function information (a frame) to be stored on a stack (the Python call stack), and a new frame to be created for the function being called. That all takes time and memory:

    >>> from timeit import timeit
    >>> def f(): pass
    ...
    >>> timeit(f, number=10**7)
    0.8021022859902587
    

    There is also a (smaller) cost of looking up the attribute (methods are attributes too), and creating the method object (each attribute lookup for a method name causes a new method object to be created):

    >>> class Foo:
    ...     bar = None
    ...     def baz(self): pass
    ...
    >>> timeit('instance.bar', 'from __main__ import Foo; instance = Foo()', number=10**7)
    0.238075322995428
    >>> timeit('instance.baz', 'from __main__ import Foo; instance = Foo()', number=10**7)
    0.3402297169959638
    

    So the sum cost of attribute lookup, method object creation and call stack operations add up to the extra time requirements you observed.

提交回复
热议问题