Skip to content

Instantly share code, notes, and snippets.

@sgillies
Last active February 7, 2019 01:02
Show Gist options
  • Save sgillies/bfeb6b479bfd2e6a0a38 to your computer and use it in GitHub Desktop.
Save sgillies/bfeb6b479bfd2e6a0a38 to your computer and use it in GitHub Desktop.
GeoJSON: json vs msgpack vs shapely.wkb vs geobuf
# Comparing serialization of complex GeoJSON geometries using:
#
# - standard lib json, marshal, pickle, cPickle
# - umsgpack
# - shapely.wkb
# - geobuf (protobuf)
#
# The test case is a nearly circular polygon with 128 vertices.
#
# Python 2.7 because geobuf isn't possible on Python 3 (because
# protobuf).
import json
import marshal
import cPickle
import pickle
import timeit
import zlib
import geobuf
import msgpack
from shapely.geometry import mapping, Point, shape
from shapely import wkb
import umsgpack
def time_roundtrip(serializer, imports, statement):
t = timeit.Timer(
statement,
setup="from __main__ import geom, {0}; eval('{1}')".format(
imports, statement))
print(serializer)
print("%.2f usec/pass" % (1000000 * t.timeit(number=1000)/1000))
print("")
geom = mapping(Point(0, 0).buffer(1.0, 32))
for serializer, imports, statement in [
('json', 'json', 'json.loads(json.dumps(geom))'),
('marshal', 'marshal', 'marshal.loads(marshal.dumps(geom))'),
('msgpack', 'msgpack', 'msgpack.loads(msgpack.dumps(geom))'),
('cPickle', 'cPickle', 'cPickle.loads(cPickle.dumps(geom))'),
('pickle', 'pickle', 'pickle.loads(pickle.dumps(geom))'),
('umsgpack', 'umsgpack', 'umsgpack.loads(umsgpack.dumps(geom))'),
('wkb', 'shape, wkb', 'wkb.loads(wkb.dumps(shape(geom)))'),
('geobuf', 'geobuf', 'geobuf.decode(geobuf.encode(geom))'),
('msgpack+zlib', 'msgpack, zlib',
'msgpack.loads(zlib.decompress(zlib.compress(msgpack.dumps(geom))))'),
('json+zlib', 'json, zlib',
'json.loads(zlib.decompress(zlib.compress(json.dumps(geom))))')
]:
time_roundtrip(serializer, imports, statement)
json
361.57 usec/pass
marshal
17.71 usec/pass
msgpack
49.55 usec/pass
cPickle
266.43 usec/pass
pickle
1558.78 usec/pass
umsgpack
1146.17 usec/pass
wkb
68.81 usec/pass
geobuf
2189.62 usec/pass
msgpack+zlib
169.66 usec/pass
json+zlib
564.29 usec/pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment