Created
December 30, 2024 10:53
-
-
Save samuelcolvin/0ee0a961885b7c9388651de4b860feae to your computer and use it in GitHub Desktop.
This file contains 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
PointPydanticBaseModel v2.10.4: 538ns | |
PointPydanticTypedDict v2.10.4: 247ns | |
attrs v24.3.0: 551ns |
This file contains 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
# /// script | |
# requires-python = ">=3.12" | |
# dependencies = [ | |
# "attrs==24.3.0", | |
# "pydantic==2.10.4", | |
# ] | |
# /// | |
import timeit | |
from typing import TypedDict | |
from pydantic import BaseModel, TypeAdapter, __version__ as pydantic_version | |
from attrs import define, field, validators, __version__ as attrs_version | |
data = dict(x=1.0, y=2.0, flag=True) | |
class PointPydanticBaseModel(BaseModel): | |
x: float | |
y: float | |
flag: bool | |
timer = timeit.Timer( | |
'PointPydanticBaseModel(**data)', setup='', globals={'PointPydanticBaseModel': PointPydanticBaseModel, 'data': data} | |
) | |
n, t = timer.autorange() | |
iter_time = (t / n) * 1_000_000_000 | |
print(f'PointPydanticBaseModel v{pydantic_version}: {iter_time:.0f}ns') | |
class PointPydanticTypedDict(TypedDict): | |
x: float | |
y: float | |
flag: bool | |
pydantic_ta = TypeAdapter(PointPydanticTypedDict) | |
timer = timeit.Timer( | |
'pydantic_ta.validate_python(data)', setup='', globals={'pydantic_ta': pydantic_ta, 'data': data} | |
) | |
n, t = timer.autorange() | |
iter_time = (t / n) * 1_000_000_000 | |
print(f'PointPydanticTypedDict v{pydantic_version}: {iter_time:.0f}ns') | |
@define | |
class PointAttrs: | |
x: float = field(validator=validators.instance_of(float)) | |
y: float = field(validator=validators.instance_of(float)) | |
flag: bool = field(validator=validators.instance_of(bool)) | |
timer = timeit.Timer( | |
'PointAttrs(**data)', setup='', globals={'PointAttrs': PointAttrs, 'data': data} | |
) | |
n, t = timer.autorange() | |
iter_time = (t / n) * 1_000_000_000 | |
print(f' attrs v{attrs_version}: {iter_time:.0f}ns') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment