dataclass vs pydantic: python version performance
on this page
Overview
python 3.13 introduced significant performance improvements for dataclasses. this benchmark tracks performance evolution from python 3.10 through 3.14.
Methodology
three operations tested with 10,000 iterations each:
- creation: object instantiation
- modification: attribute updates
- serialization: conversion to dictionary
Test Implementation
# Example test structure
@dataclass
class PersonDataclass:
name: str
age: int
email: str
active: bool = True
class PersonPydantic(BaseModel):
name: str
age: int
email: str
active: bool = True
timing via time.perf_counter()
. complete benchmark: benchmark.py
Results
Summary
- python 3.13 improved dataclass creation by 3x and serialization by 4x
- pydantic performance remained consistent across versions
- pydanticβs serialization advantage decreased from 6x to 1.3x
Performance Data
python | operation | dataclasses (ΞΌs) | pydantic (ΞΌs) | relative |
---|---|---|---|---|
3.10 | creation | 0.90 | 0.85 | 0.9x |
modification | 2.02 | 1.94 | 1.0x | |
serialization | 4.48 | 0.77 | 0.2x | |
3.13 | creation | 0.33 | 0.85 | 2.6x |
modification | 1.56 | 1.75 | 1.1x | |
serialization | 1.01 | 0.70 | 0.7x | |
3.14 | creation | 0.33 | 0.93 | 2.8x |
modification | 1.47 | 1.80 | 1.2x | |
serialization | 1.04 | 0.79 | 0.8x |
lower times indicate better performance. relative = pydantic / dataclass
Scripts
benchmark.py
- benchmark runneranalyze.py
- data analysisvisualize_versions.py
- visualizationresults_python_*.json
- raw results per version
Recommendations
- on python 3.13+, performance differences are minimal - choose based on features
- for serialization-heavy workloads, pydantic maintains an advantage
- for simple data containers, dataclasses offer excellent performance with no dependencies
Notes
- python 3.14 requires pydantic 2.12.0a1 or later (pre-release)
- all benchmarks run on identical hardware
- measurements include all overhead (validation, type checking)
See Also
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ