Created
May 12, 2016 19:40
-
-
Save brunsgaard/1291b8bfd02c4d0df2e706290c96ce0a 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): | |
print('Testing {}: '.format(name)) | |
build = build.format(n=entries) | |
build_times = timeit.repeat(repeat=1, number=number, stmt=build, setup=setup) | |
print('{:<20} {:>6} 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} {:>6} 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} {:>6} ms'.format('deserialize:', int(min(deserialize_times) * 1000))) | |
exec(setup) | |
data_ = locals().get('data') | |
total = int(1000 * sum([min(l) for l in [build_times, serialize_times, deserialize_times]])) | |
print('{:<20} {:>6} ms'.format('totaltime:', total)) | |
print('{:<20} {:>6} MB\n'.format('size:', round(len(data_) / (1024*1024) * number, 2))) | |
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') | |
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment