Why is creating a class in Python so much slower than instantiating a class?

前端 未结 4 1272
谎友^
谎友^ 2021-01-31 17:33

I found that creation of a class is way slower than instantiation of a class.

>>> from timeit import Timer as T
>>> def calc(n):
...     return         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 18:03

    This might give you the intuition:

    >>> class Haha(object): pass
    ...
    >>> sys.getsizeof(Haha)
    904
    >>> sys.getsizeof(Haha())
    64
    

    Class object is much more complex and expensive structure than an instance of that class.

提交回复
热议问题