Skip to content

Instantly share code, notes, and snippets.

@paveltyavin
Last active June 7, 2016 20:05
Show Gist options
  • Save paveltyavin/b837bc997d61b6a778c2f415eeabcfd6 to your computer and use it in GitHub Desktop.
Save paveltyavin/b837bc997d61b6a778c2f415eeabcfd6 to your computer and use it in GitHub Desktop.
typedict vs schematics benchmark
typeddict_test       1.759s        372 Kb
schematics_test 2.0  3.188s        152 Kb
schematics_test 1.1  3.339s         96 Kb
import time
import resource
from resource import getrusage
import datetime
from schematics.exceptions import ModelConversionError
from schematics.types.compound import ListType, ModelType
from typeddict.typedbase.typeddict import TypedDict
from typeddict.api import f, opt, ref, tlist
from typeddict.validators import ValidationError
from schematics.models import Model
from schematics.types import StringType, IntType, BooleanType, DateTimeType
class NestedTypeDict(TypedDict):
s = f.String()
i = f.Int()
b = f.Bool()
class TestTypedDict(TypedDict):
s = f.String()
i = f.Int()
b = f.Bool()
l_int = tlist(f.Int)
s_opt = opt(f.String)
i_opt = opt(f.Int)
b_opt = opt(f.Bool)
d_opt = opt(f.DateTime)
n = ref(NestedTypeDict)
class NestedSchematics(Model):
s = StringType(required=True)
i = IntType(required=True)
b = BooleanType(required=True)
class TestSchematics(Model):
s = StringType(required=True)
i = IntType(required=True)
b = BooleanType(required=True)
l_int = ListType(IntType)
s_opt = StringType(required=False)
i_opt = IntType(required=False)
b_opt = BooleanType(required=False)
d_opt = DateTimeType(required=False)
n = ModelType(NestedSchematics)
def print_time(fn):
def wrapper():
t1 = time.clock()
m1 = getrusage(resource.RUSAGE_SELF).ru_maxrss
for _ in range(10000):
fn()
t2 = time.clock()
m2 = getrusage(resource.RUSAGE_SELF).ru_maxrss
print("{: <20} {:.3f}s {: 10d} Kb".format(
fn.__name__,
t2 - t1,
(m2 - m1) / 1024,
))
return wrapper
full_data = {
's': '123',
'i': 123,
'b': True,
'l_int': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
's_opt': 's_opt',
'i_opt': 321,
'b_opt': False,
'd_opt': datetime.datetime(2010, 1, 1),
'n': {
's': '123',
'i': 123,
'b': True,
}
}
good_data = {
's': '123',
'l_int': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'i': 123,
'b': True,
'n': {
's': '123',
'i': 123,
'b': True,
}
}
bad_data = {
's': False,
'i': False,
'b': 123,
}
@print_time
def typeddict_test():
TestTypedDict(**good_data)
TestTypedDict(**full_data)
try:
TestTypedDict(**bad_data)
except ValidationError:
pass
@print_time
def schematics_test():
TestSchematics(good_data)
TestSchematics(full_data)
try:
TestSchematics(bad_data)
except ModelConversionError:
pass
if __name__ == '__main__':
typeddict_test()
schematics_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment