Skip to content

Instantly share code, notes, and snippets.

@gerryjenkinslb
Last active August 24, 2018 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerryjenkinslb/17ec35e6f2898cf8c34779eedbf1636f to your computer and use it in GitHub Desktop.
Save gerryjenkinslb/17ec35e6f2898cf8c34779eedbf1636f to your computer and use it in GitHub Desktop.
''' comparing speed of new dataclass
with standard class attributes and slots,
running in 3.7.0b1'''
# subscribe to me: https://www.youtube.com/user/gjenkinslbcc?sub_confirmation=1
# I recommend reading PEP557: https://www.python.org/dev/peps/pep-0557/
# this code at gist: http://bit.ly/slotsVdatclass
from dataclasses import dataclass
from timeit import timeit
def _str(p): return f'({p.x}, {p.y}, {p.z})' # shared str()
class PointStdClass: # standard class
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x, self.y, self.z = x, y, z
def __str__(self): return _str(self)
class PointSlots: # slots based class
__slots__ = 'x','y','z'
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x, self.y, self.z = x, y, z
def __str__(self): return _str(self)
@dataclass
class PointDataClass: # DataClass based class, note __init__ is predefined, see PEP557 for more predefined
x: float = 0.0
y: float = 0.0
z: float = 0.0
def __str__(self): return _str(self)
# create some of each
c1, c2 = PointStdClass(), PointStdClass(1.0, 2.0, 3.0)
s1, s2 = PointSlots(), PointSlots(1.0, 2.0, 3.0)
dc1, dc2 = PointDataClass(), PointDataClass(1.0, 2.0, 3.0)
# print them
print(c1, c2)
print(s1,s2)
print(dc1, dc2)
# timeit: create instance, access, assignment
print('\nPoint object Creation:')
t_class = timeit('PointStdClass()',setup='from __main__ import PointStdClass')
t_slots = timeit('PointSlots()',setup='from __main__ import PointSlots')
t_dataclass = timeit('PointDataClass()',setup='from __main__ import PointDataClass')
print(f' standard class: {t_class:.6f}')
print(f' slots class : {t_slots:.6f}')
print(f' dataclass : {t_dataclass:.6f}')
print('\nPoint attribute access:')
t_class = timeit('p.x',setup='from __main__ import PointStdClass; p = PointStdClass()')
t_slots = timeit('p.x',setup='from __main__ import PointSlots; p = PointSlots()')
t_dataclass = timeit('p.x',setup='from __main__ import PointDataClass; p = PointDataClass()')
print(f' standard class: {t_class:.6f}')
print(f' slots class : {t_slots:.6f}')
print(f' dataclass : {t_dataclass:.6f}')
print('\nPoint attribute assignment:')
t_class = timeit('p.x = 9.0',setup='from __main__ import PointStdClass; p = PointStdClass()')
t_slots = timeit('p.x = 9.0',setup='from __main__ import PointSlots; p = PointSlots()')
t_dataclass = timeit('p.x = 9.0',setup='from __main__ import PointDataClass; p = PointDataClass()')
print(f' standard class: {t_class:.6f}')
print(f' slots class : {t_slots:.6f}')
print(f' dataclass : {t_dataclass:.6f}')
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment