Skip to content

Instantly share code, notes, and snippets.

@jimfulton
Created April 6, 2013 17:58
Show Gist options
  • Save jimfulton/5326984 to your computer and use it in GitHub Desktop.
Save jimfulton/5326984 to your computer and use it in GitHub Desktop.
Informal comparison of pickling speeds across Python versions. Python 3's pickling speed improvements are impressive, especially given the transparency of the C optimizations.
import sys, time
if sys.version_info[0] == 2:
import cPickle as pickle
else:
import pickle
class C(object):
def __init__(self, i):
self.name = 'my object'
self.age = i
self.weight = 123.456
self.hobbies = 'tennis hacking skiing cooking coffee'.split()
data = [C(i) for i in range(99999)]
def test1():
with open('data', 'wb') as f:
p = pickle.Pickler(f, 1)
s = time.time()
for c in data:
p.dump(c)
print("test1 pickle", time.time() - s)
f.close()
with open('data', 'rb') as f:
p = pickle.Unpickler(f)
s = time.time()
for c in data:
x = p.load()
print("test1 unpickle", time.time() - s)
f.close()
def test2():
with open('data', 'wb') as f:
p = pickle.Pickler(f, 1)
s = time.time()
p.dump(data)
print("test2 pickle", time.time() - s)
f.close()
with open('data', 'rb') as f:
p = pickle.Unpickler(f)
s = time.time()
x = p.load()
print("test2 unpickle", time.time() - s)
f.close()
if __name__ == "__main__":
test1()
test2()
python2.6 ptest.py
('test1 pickle', 3.6531879901885986)
('test1 unpickle', 1.5503621101379395)
('test2 pickle', 3.5267090797424316)
('test2 unpickle', 1.563115119934082)
python2.7 ptest.py
('test1 pickle', 2.829061985015869)
('test1 unpickle', 1.4226188659667969)
('test2 pickle', 2.792673110961914)
('test2 unpickle', 1.226505994796753)
python3.2 ptest.py
test1 pickle 1.8405969142913818
test1 unpickle 1.2779920101165771
test2 pickle 1.4400849342346191
test2 unpickle 1.0933279991149902
python3.3 ptest.py
test1 pickle 1.5386929512023926
test1 unpickle 1.0352110862731934
test2 pickle 1.2123191356658936
test2 unpickle 0.8641130924224854
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment