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 82 83 84
| from timeit import Timer
from datetime import datetime
def rep(dt):
return str(dt).replace('-','/')
def dkp(dt):
sdt =str(dt)
return sdt[0:4]+'/'+sdt[5:7]+'/'+sdt[8:]
def strf(dt):
return dt.strftime("%Y/%m/%d %H:%M:%S.")+str(dt)[-6:]
dt = datetime(2009,12,13,4,0,6,237800)
print 'dt =',dt,' ',type(dt)
print 'dkp(dt) =',dkp(dt)
print 'rep(dt) =',rep(dt)
print 'strf(dt) =',strf(dt)
print
repet = 3000
iterat = 20
tr = Timer("rep(dt)","from __main__ import dt,rep").repeat(repet,iterat)
td = Timer("dkp(dt)",'from __main__ import dt,dkp').repeat(repet,iterat)
ts = Timer('strf(dt)','from __main__ import dt,strf').repeat(repet,iterat)
mr = sum(tr)/len(tr)
trslim = []
traber = []
for u in tr:
if u < 2*mr:
trslim.append(u)
else:
traber.append(u)
mrslim = sum(trslim)/len(trslim)
print '\nmoyenne replace :'
print ('len de tr ='+str(len(tr))).rjust(20),('len de tr sans valeurs aberrantes ='+str(len(trslim))).rjust(47)
print str(mr).rjust(20),str(mrslim).rjust(47)
print "liste des rapports 'valeurs aberrantes de temps / moyenne' :"
for el in [str(u/mrslim)[0:4] for u in traber]:
print el+' - ',
print '\n'
md = sum(td)/len(td)
tdslim = []
tdaber = []
for u in td:
if u < 2*md:
tdslim.append(u)
else:
tdaber.append(u)
mdslim = sum(tdslim)/len(tdslim)
print 'moyenne decoupe :'
print ('len de td ='+str(len(td))).rjust(20),('len de td sans valeurs aberrantes ='+str(len(tdslim))).rjust(47)
print str(md).rjust(20),str(sum(tdslim)/len(tdslim)).rjust(47)
print "liste des rapports 'valeurs aberrantes de temps / moyenne' :"
for el in [str(u/mdslim)[0:4] for u in traber]:
print el+' - ',
print '\n'
ms = sum(ts)/len(ts)
tsslim = []
tsaber = []
for u in ts:
if u < 2*ms:
tsslim.append(u)
else:
tsaber.append(u)
msslim = sum(tsslim)/len(tsslim)
print '\nmoyenne strftime :'
print ('len de ts ='+str(len(ts))).rjust(20),('len de ts sans valeurs aberrantes ='+str(len(tsslim))).rjust(47)
print str(ms).rjust(20),str(sum(tsslim)/len(tsslim)).rjust(47)
print "liste des rapports 'valeurs aberrantes de temps / moyenne' :"
for el in [str(u/msslim)[0:4] for u in tsaber]:
print el+' - ',
print '\n'
print
print 'moyenne strftime / moyenne replace : ',ms/mr,' ',(msslim/mrslim) |