DEV Community

jneeee
jneeee

Posted on

Dataclasses have better performace than Namedtuple

Dataclasses exhibit superior performance compared to Namedtuple, contrary to my initial belief.

Below is the code snippet. I will create 1,000,000 instances for each class, comparing the performance of dataclasses with slot=True versus slot=False to determine which configuration is superior.

import time
import sys
from typing import NamedTuple
from dataclasses import dataclass

class PointNamedTuple(NamedTuple):
    x: int
    y: int

@dataclass
class PointDataClass:
    x: int
    y: int

@dataclass(slots=True)
class PointDataClassSlots:
    x: int
    y: int

NUM_OBJECTS = 1_000_000


def test_create_instance(func, num_objects):
    start = time.perf_counter()
    _list = [func(i, i) for i in range(num_objects)]
    _size = sys.getsizeof(_list[0])
    print(f"{func.__name__} instance {num_objects} objects: {time.perf_counter() - start:.5f} seconds, {_size} bytes")
    return time.perf_counter() - start, _size

test_create_instance(PointNamedTuple, NUM_OBJECTS)
test_create_instance(PointDataClass, NUM_OBJECTS)
test_create_instance(PointDataClassSlots, NUM_OBJECTS)
Enter fullscreen mode Exit fullscreen mode

The result

PointDataClassSlots instance 1000000 objects: 0.68315 seconds, 8448728 bytes
PointDataClass instance 1000000 objects: 0.98761 seconds, 8448728 bytes
PointNamedTuple instance 1000000 objects: 1.01048 seconds, 8448728 bytes
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jneeee profile image
jneeee

New pydantic test result

class PointBaseModel(BaseModel):
    x: int
    y: int



tests/test_pydantic.py::test_create_instance[PointNamedTuple] 0.32363s, 0.0546875 Bit
PASSED
tests/test_pydantic.py::test_create_instance[PointDataClass] 0.28006s, 0.046875 Bit
PASSED
tests/test_pydantic.py::test_create_instance[PointDataClassSlots] 0.26146s, 0.046875 Bit
PASSED
tests/test_pydantic.py::test_create_instance[PointBaseModel] 1.16649s, 0.0703125 Bit
PASSED
Enter fullscreen mode Exit fullscreen mode