Skip to content

Instantly share code, notes, and snippets.

@brunsgaard
Created May 11, 2016 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunsgaard/aafc51e072467739c847b840fde49a75 to your computer and use it in GitHub Desktop.
Save brunsgaard/aafc51e072467739c847b840fde49a75 to your computer and use it in GitHub Desktop.
import timeit
def test(setup, build, serialize, deserialize, number, entries, name):
build = build.format(n=entries)
build_times = timeit.repeat(repeat=1, number=number, stmt=build, setup=setup)
print('{:<20} {:>5} ms'.format('build datastructure:', int(min(build_times) * 1000)))
setup = setup + build
serialize_times = timeit.repeat(repeat=1, number=number, stmt=serialize, setup=setup)
print('{:<20} {:>5} ms'.format('serialize:', int(min(serialize_times) * 1000)))
setup = setup + serialize
deserialize_times = timeit.repeat(repeat=1, number=number, stmt=deserialize, setup=setup)
print('{:<20} {:>5} ms'.format('deserialize:', int(min(deserialize_times) * 1000)))
exec(setup)
data_ = locals().get('data')
print('{:<20} {:>5} MB'.format('size:', round(len(data_) / (1024*1024) * number, 2)))
print('{:<20} {:>5} ms\n'.format('totaltime:', int(1000 * sum([min(l) for l in [build_times, serialize_times, deserialize_times]]))))
setup = '''\
import capnp
import addressbook_capnp
import warnings
warnings.simplefilter("ignore")
'''
build = '''\
addresses = addressbook_capnp.AddressBook.new_message()
people = addresses.init('people', {n})
for i in range({n}):
alice = people[i]
alice.id = 123
alice.name = 'Alice'
alice.email = 'alice@example.com'
alicePhones = alice.init('phones', 1)
alicePhones[0].number = "555-1212"
alicePhones[0].type = 'mobile'
'''
serialize = '''\
data = addresses.to_bytes_packed()
'''
deserialize = '''\
addressbook_capnp.Person.from_bytes_packed(data, traversal_limit_in_words=1024*1024*32)
'''
# test(setup, build, serialize, deserialize, 1000, 1000, 'Capnp')
setup = """\
import addressbook_pb2
"""
build = '''\
address_book = addressbook_pb2.AddressBook()
for i in range({n}):
person = address_book.person.add()
person.id = 123
person.name = 'Alice'
person.email ='alice@example.com'
phone_number = person.phone.add()
phone_number.number = "555-1212"
phone_number.type = addressbook_pb2.Person.MOBILE
'''
serialize = '''\
data = address_book.SerializeToString()
'''
deserialize = '''\
address_book.ParseFromString(data)
'''
# test(setup, build, serialize, deserialize, 1000, 1000, 'Protobuf')
setup = """\
import msgpack
"""
build = '''\
addresses = [{{
'id': 123,
'name': 'Alice',
'email': 'alice@example.com',
'phones': {{'number': "555-1212", 'type': 'mobile'}},
}} for _ in range({n})]
'''
serialize = '''\
data = msgpack.packb(addresses)
'''
deserialize = '''\
msgpack.unpackb(data)
'''
test(setup, build, serialize, deserialize, 1000, 1000, 'MsgPack')
@brunsgaard
Copy link
Author

brunsgaard commented May 11, 2016

Capnp:
build datastructure:  2807 ms
serialize:             229 ms
deserialize:           142 ms
size:                70.77 MB
totaltime:            3179 ms

Protobuf:
build datastructure: 15161 ms
serialize:           18928 ms
deserialize:         27535 ms
size:                41.96 MB
totaltime:           61626 ms

Msgpack:
build datastructure:   537 ms
serialize:            1503 ms
deserialize:          1045 ms
size:                72.48 MB
totaltime:            3086 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment