Python 类型注释会减慢代码速度

Sad*_*deh 7 type-hinting cprofile python-3.x python-dataclasses

我正在优化我的代码以提高性能,当我用来cProfile检查我的代码时,大量的运行时间是由于类型注释造成的!删除类型注释确实可以提高性能。cProfiler您可以在下面看到带注释和不带注释的输出。

注释代码: 带注释的代码性能

未注释的代码: 没有注释的代码的性能

带注释的明显使用了__call____new__inner__getitem____hash__等方法typing.py,而且比不带注释的慢了一倍!

我的测试代码很简单:

from reil.datatypes import reildata

x = reildata.Categorical(name='cat', categories=('A', 'B', 'C', 'D', 'E'))

for _ in range(10000):
    [x(v) for v in ('A', 'B', 'C', 'D', 'E')]
Run Code Online (Sandbox Code Playgroud)

这是主代码的相关部分(datatypes.reildata.py):

from __future__ import annotations

import dataclasses
import itertools
from dataclasses import field
from typing import Any, Callable, Dict, Generic, Iterable, Iterator, List, Optional, Sequence, Tuple, TypeVar, Union, cast

from typing_extensions import Literal


T = TypeVar('T')

CategoricalType = TypeVar('CategoricalType')

Normal = Union[Literal[0], Literal[1], float]
Normalized = Union[Normal, Tuple[Normal, ...], None]


@dataclasses.dataclass(frozen=True)
class ReilSingleton(Generic[T]):
    name: str
    value: Optional[Union[T, Tuple[T, ...]]] = None
    is_numerical: Optional[bool] = field(
        repr=False, compare=False, default=None)
    normalized: Normalized = field(
        default=None, repr=False, compare=False)


@dataclasses.dataclass(frozen=True)
class Categorical(Generic[CategoricalType]):
    name: str
    categories: Optional[Tuple[CategoricalType, ...]] = None
    normal_form: Optional[Dict[CategoricalType, Tuple[Normal, ...]]] = field(
        default=None, init=False, repr=False, compare=False)

    def __post_init__(self):
        if self.categories is None:
            return

        cat_count = len(self.categories)
        normal_form = {}
        for i, c in enumerate(self.categories):
            temp = [0] * cat_count
            temp[i] = 1
            normal_form[c] = tuple(temp)

        self.__dict__['normal_form'] = normal_form

    def __call__(self,
                 data: Union[CategoricalType, Tuple[CategoricalType, ...]]
                 ) -> ReilSingleton:
        normal_form = self.normal_form
        categories = cast(Tuple[CategoricalType, ...], self.categories)

        if normal_form is None:
            normalized = None
        elif data in categories:
            normalized = normal_form[cast(CategoricalType, data)]
        else:
            try:
                normalized = tuple(
                    itertools.chain(
                        *(normal_form[d]
                          for d in cast(Tuple[CategoricalType, ...], data))))
            except KeyError:
                raise ValueError(
                    f'{data} is not '
                    f'in the categories={categories}.')

        instance = ReilSingleton[CategoricalType](
            name=self.name, is_numerical=False, value=data,
            normalized=normalized)
        instance.__dict__['categories'] = categories
        instance.__dict__['dict_fields'] = ('name', 'value', 'categories')

        return instance
Run Code Online (Sandbox Code Playgroud)

如果我想保留注释该怎么办?难道我做错了什么?我的 Python 解释器是 Python 3.7.8 (cPython),我在 Windows 10 上运行测试。