Skip to content

Instantly share code, notes, and snippets.

@gijs
Forked from sgillies/benchmark.py
Last active September 21, 2015 07:58
Show Gist options
  • Save gijs/ec330c7a2e0fc09efc83 to your computer and use it in GitHub Desktop.
Save gijs/ec330c7a2e0fc09efc83 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 geobuf
from shapely.geometry import mapping, Point, shape
from shapely import wkb
import simplejson
import umsgpack
def time_roundtrip(serializer, statement):
setup = 'from __main__ import geom, shape, {0}'.format(serializer)
t = timeit.Timer(statement, setup=setup)
print("{0} loads/dumps".format(serializer))
print("%.2f usec/pass" % (1000000 * t.timeit(number=1000)/1000))
print("")
geom = mapping(Point(0, 0).buffer(1.0, 32))
for serializer, statement in {
'json': 'json.loads(json.dumps(geom))',
'marshal': 'marshal.loads(marshal.dumps(geom))',
'cPickle': 'cPickle.loads(cPickle.dumps(geom))',
'pickle': 'pickle.loads(pickle.dumps(geom))',
'umsgpack': 'umsgpack.loads(umsgpack.dumps(geom))',
'wkb': 'wkb.loads(wkb.dumps(shape(geom)))',
'geobuf': 'geobuf.decode(geobuf.encode(geom))'}.items():
time_roundtrip(serializer, statement)
$ python benchmark.py
json loads/dumps
386.17 usec/pass
marshal loads/dumps
17.83 usec/pass
cPickle loads/dumps
275.89 usec/pass
pickle loads/dumps
1603.66 usec/pass
umsgpack loads/dumps
1156.33 usec/pass
shapely loads/dumps
413.24 usec/pass
geobuf loads/dumps
2199.46 usec/pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment