Skip to content

Instantly share code, notes, and snippets.

@sublee
Created June 18, 2013 05:25
Show Gist options
  • Save sublee/5802836 to your computer and use it in GitHub Desktop.
Save sublee/5802836 to your computer and use it in GitHub Desktop.
protobuf vs. msgpack in Python
# -*- coding: utf-8 -*-
import time
import msgpack
from person_pb2 import Person
TIMES = 100000
def make_person(size):
p = Person()
p.id = 1
p.name = 'Heungsub Lee'
p.email = '[email protected]'
for x in xrange(size):
p.lucky_numbers.append(x)
return p
def make_dict(size):
return {'id': 1, 'name': 'Heungsub Lee', 'email': '[email protected]',
'lucky_numbers': range(size)}
def make_tuple(size):
return (1, 'Heungsub Lee', '[email protected]', range(size))
def protobuf_pack(times, size):
p = make_person(size)
t = time.time()
for x in xrange(times):
p.SerializeToString()
return time.time() - t, len(p.SerializeToString())
def protobuf_unpack(times, size):
s = make_person(size).SerializeToString()
p = Person()
t = time.time()
for x in xrange(times):
p.ParseFromString(s)
return time.time() - t
def msgpack_pack(times, size, make=make_dict):
d = make(size)
t = time.time()
for x in xrange(times):
msgpack.packb(d)
return time.time() - t, len(msgpack.packb(d))
def msgpack_unpack(times, size, make=make_dict):
b = msgpack.packb(make(size))
t = time.time()
for x in xrange(times):
msgpack.unpackb(b)
return time.time() - t
if __name__ == '__main__':
fmt = '{0:,}ns/pack {1:,}ns/unpack ({2:,} bytes)'
ns = lambda d: d / TIMES * 10 ** 9
for size in [0, 10, 100, 1000]:
print 'array size: {}'.format(size)
pack_duration, length = protobuf_pack(TIMES, size)
unpack_duration = protobuf_unpack(TIMES, size)
print 'protobuf',
print fmt.format(ns(pack_duration), ns(unpack_duration), length)
pack_duration, length = msgpack_pack(TIMES, size)
unpack_duration = msgpack_unpack(TIMES, size)
print 'msgpack with dict',
print fmt.format(ns(pack_duration), ns(unpack_duration), length)
pack_duration, length = msgpack_pack(TIMES, size, make=make_tuple)
unpack_duration = msgpack_unpack(TIMES, size, make=make_tuple)
print 'msgpack with tuple',
print fmt.format(ns(pack_duration), ns(unpack_duration), length)
# array size: 0
# protobuf 12,794ns/pack 7,402ns/unpack (33 bytes)
# msgpack with dict 902ns/pack 517ns/unpack (60 bytes)
# msgpack with tuple 679ns/pack 286ns/unpack (32 bytes)
# array size: 10
# protobuf 23,731ns/pack 31,589ns/unpack (53 bytes)
# msgpack with dict 1,107ns/pack 620ns/unpack (70 bytes)
# msgpack with tuple 870ns/pack 436ns/unpack (42 bytes)
# array size: 100
# protobuf 112,659ns/pack 189,907ns/unpack (233 bytes)
# msgpack with dict 2,598ns/pack 1,533ns/unpack (162 bytes)
# msgpack with tuple 2,200ns/pack 1,291ns/unpack (134 bytes)
# array size: 1000
# protobuf 1,335,493ns/pack 2,117,658ns/unpack (2,905 bytes)
# msgpack with dict 18,968ns/pack 18,040ns/unpack (2,678 bytes)
# msgpack with tuple 16,929ns/pack 18,499ns/unpack (2,650 bytes)
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
repeated int32 lucky_numbers = 4;
}
@yoloseem
Copy link

결론: "msgpack이 훨씬 빠르고 바이트도 적게 쓴다"가 맞나요?

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