Instantly share code, notes, and snippets.

Embed
What would you like to do?
import StringIO
import cPickle
small = [{i: i} for i in xrange(9)]
medium = [{i: i} for i in xrange(999)]
large = [{i: i} for i in xrange(999999)]
def dumps(obj):
return cPickle.dumps(obj)
def dumps_highest_protocol(obj):
return cPickle.dumps(obj, cPickle.HIGHEST_PROTOCOL)
def dumps_fast_mode(obj):
buff = StringIO.StringIO()
pickler = cPickle.Pickler(buff, cPickle.HIGHEST_PROTOCOL)
pickler.fast = 1
pickler.dump(obj)
buff.flush()
return buff.getvalue()
#!/bin/sh
echo 'SMALL'
echo 'dumps' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps(p.small)"
echo 'highp' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps_highest_protocol(p.small)"
echo 'pickl' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps_fast_mode(p.small)"
echo
echo 'MEDIUM'
echo 'dumps' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps(p.medium)"
echo 'highp' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps_highest_protocol(p.medium)"
echo 'pickl' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps_fast_mode(p.medium)"
echo
echo 'LARGE'
echo 'dumps' && python -m timeit -n 3 -s "import pickle_experiments as p" "p.dumps(p.large)"
echo 'highp' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps_highest_protocol(p.large)"
echo 'pickl' && python -m timeit -n 10 -s "import pickle_experiments as p" "p.dumps_fast_mode(p.large)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment