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)
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