Skip to content

Instantly share code, notes, and snippets.

@Bobronium
Created August 5, 2019 09:53
Show Gist options
  • Save Bobronium/78fd344d989a541cfd7cced3094f3154 to your computer and use it in GitHub Desktop.
Save Bobronium/78fd344d989a541cfd7cced3094f3154 to your computer and use it in GitHub Desktop.
from dataclasses import dataclass
from enum import Enum
from timeit import timeit
from typing import NamedTuple
from pydantic import BaseModel
from pydantic.dataclasses import dataclass as pydantic_dataclass
class Model(BaseModel):
name: str
@pydantic_dataclass()
class PDDataClass:
name: str
@dataclass()
class DataClass:
name: str
class NT(NamedTuple):
name: str
class EM(str, Enum):
name: str = 'foo'
class Class:
name: str = 'foo'
class Inst:
name: str
def __init__(self, name):
self.name = name
m = Model(name='foo')
pd_dc = PDDataClass(name='foo')
dc = DataClass(name='foo')
nt = NT(name='foo')
inst = Inst(name='foo')
m_gl = {'m': m}
py_dc_gl = {'pd_dc': pd_dc}
dc_gl = {'dc': dc}
nt_gl = {'nt': nt}
em_gl = {'EM': EM}
cls_gl = {'cls': Class}
inst_gl = {'inst': inst}
num = 100000
print(
'Testing pydantic.BaseModel:',
timeit('m.name', globals=m_gl, number=num),
'Testing pydantic.dataclasses.dataclass:',
timeit('pd_dc.name', globals=py_dc_gl, number=num),
'Testing dataclass:',
timeit('dc.name', globals=dc_gl, number=num),
'Testing NamedTuple:',
timeit('nt.name', globals=nt_gl, number=num),
'Testing Enum',
timeit('EM.name', globals=em_gl, number=num),
'Testing ordinal Class:',
timeit('cls.name', globals=cls_gl, number=num),
'Testing ordinal Class Instance',
timeit('inst.name', globals=inst_gl, number=num),
sep='\n'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment