1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #!/usr/bin/env python
#coding=utf-8
from time import clock
import pickle, cPickle, shelve
# The following script makes a little comparison between
# the use of pickle, cPickle and shelve in a very simple context.
timeMemo = {'pickle' : [0, 0], 'cpickle' : [0, 0], 'shelve' : [0, 0]}
i_max = 10000
theTitles = ['Title ' + str(i) for i in range(1, i_max)]
print '=== PICKLE ==='
timeStart = clock()
with open("test.txt", 'w') as f:
for oneTitle in theTitles:
pickle.dump(oneTitle, f)
timeEnd = clock()
timeMemo['pickle'][0] = timeEnd-timeStart
timeStart = clock()
with open("test.txt", 'r') as f:
try:
while True:
oneVar = pickle.load(f)
#print oneVar
except:
pass
timeEnd = clock()
timeMemo['pickle'][1] = timeEnd-timeStart
print '=== CPICKLE ==='
timeStart = clock()
with open("testBis.txt", 'wb') as f:
for oneTitle in theTitles:
cPickle.dump(oneTitle, f, cPickle.HIGHEST_PROTOCOL)
timeEnd = clock()
timeMemo['cpickle'][0] = timeEnd-timeStart
timeStart = clock()
with open("testBis.txt", 'rb') as f:
try:
while True:
oneVar = cPickle.load(f)
#print oneVar
except:
pass
timeEnd = clock()
timeMemo['cpickle'][1] = timeEnd-timeStart
print '=== SHELVE ==='
timeStart = clock()
d = shelve.open('test.dbm')
numTitle = 1
for oneTitle in theTitles:
d[str(numTitle)] = oneTitle
numTitle += 1
d.close()
timeEnd = clock()
timeMemo['shelve'][0] = timeEnd-timeStart
timeStart = clock()
d = shelve.open('test.dbm')
for i in xrange(1, i_max):
oneVar = d[str(i)]
#print oneVar
d.close()
timeEnd = clock()
timeMemo['shelve'][1] = timeEnd-timeStart
for oneMethod in timeMemo.keys():
print oneMethod,
print '\n\tEcriture : ', timeMemo[oneMethod][0] ,' Lecture : ', timeMemo[oneMethod][1] |
Partager