附上测试代码 供lz参考
##############
cpickle.py #################
import cPickle as pickle
import time
start = time.clock()
db = {};
for i in range(4000000):
db[str(i)] = str(i)
f = open("cpickle.db", "wb")
pickle.dump(db, f)
f.close()
end = time.clock()
print "write: %f s" % (end - start)
start = time.clock()
f=open("cpickle.db", "rb")
db = pickle.load(f)
for i in db:
if int(i) % 1000000 == 0:
print i, db[i]
f.close()
end = time.clock()
print "read: %f s" % (end - start)
##############
gdbm.py #################
import gdbm
import time
start = time.clock()
db = gdbm.open("gdbm.db", "c")
for i in range(4000000):
db[str(i)] = str(i)
db.close()
end = time.clock()
print "write: %f s" % (end - start)
start = time.clock()
db = gdbm.open("gdbm.db", "r")
for i in db.keys():
if int(i) % 1000000 == 0:
print i, db[i]
db.close()
end = time.clock()
print "read: %f s" % (end - start)
##############
pickle.py #################
import pickle
import time
# timmer code
start = time.clock()
# real code
db = {};
for i in range(4000000):
db[str(i)] = str(i)
f = open("pickle.db", "wb");
pickle.dump(db, f)
f.close()
#timmer code
end = time.clock()
print "write: %f s" % (end - start)
start = time.clock()
f=open("pickle.db", "rb")
db = pickle.load(f)
for i in db:
if int(i) % 1000000 == 0:
print i, db[i]
f.close()
end = time.clock()
print "read: %f s" % (end - start)