I was going through data classes and named tuple. I found this behaviour where creating objects using different features of python have different performance.
dataclass:
In [1]: from dataclasses import dataclass
...:
...: @dataclass
...: class Position:
...: lon: float = 0.0
...: lat: float = 0.0
...:
In [2]: %timeit for _ in range(1000): Position(12.5, 345)
326 µs ± 34.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Normal class:
In [1]: class Position:
...:
...: def __init__(self, lon=0.0, lat=0.0):
...: self.lon = lon
...: self.lat = lat
...:
In [2]: %timeit for _ in range(1000): Position(12.5, 345)
248 µs ± 2.89 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
namedtuple:
In [2]: Position = namedtuple("Position", ["lon","lat"], defaults=[0.0,0.0])
In [3]: %timeit for _ in range(1000): Position(12.5, 345)
286 µs ± 13.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
- Python version: 3.7.3
- OS: MacOS Mojave
All implementations have same object attributes, same default values.
- Why is this trend of time(dataclass) > time(namedtuple) > time(normal class)?
- What does each implementation do to take their respective time?
- Which implementation is best performing in what scenario?
Here, time denotes time taken for creating objects.