Created
May 11, 2016 07:48
-
-
Save brunsgaard/aafc51e072467739c847b840fde49a75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Author
brunsgaard
commented
May 11, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment