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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
| from time import clock
import re
repet = 100
A,B,C,D,E,F,G,H,I = [],[],[],[],[],[],[],[],[]
for wq in xrange(3):
print '* ',
### Code 1 = du precedent message
#================================
# 2 expressions regulieres, L = liste de listes de nombres sous forme de chaine
# modification de L en entier avant enregistrement des lignes BEND
te = clock()
with open('piktux.txt','r') as f:
ch = f.read()
for y in xrange(repet):
with open('piktux.txt','r') as f:
ch = f.read()
patSpool = re.compile('/SPOOL\r?\nSPOOL-ID\s+(\d+)\r?\nSPOOL-PREFIX\s+(.+?)\r?\n'
'(.+?)/END-SPOOL',re.DOTALL)
agarder = ('^ND {23}', '^OD {23}', '^WALL-THICKNESS {11}',
'^START {9}','^BEND[\d ]{10}','^FINISH {8}')
# dans agarder: en-têtes précis des lignes à garder
patLigne = re.compile( '(' + '|'.join( ag+'.+\n' for ag in agarder ) + ')',re.MULTILINE )
for id,prefix,portion in (m.groups() for m in patSpool.finditer(ch)):
# on crée le nom du fichier qui va recevoir le contenu de cette portion
out_name = id + '^' + prefix + '.txt'
# on crée la sélection de lignes de cette portion qui va être enregistrée
chout = ''
for line in ( m.group() for m in patLigne.finditer(portion)):
if line[0:5] == 'START': # ligne START
L = [line[14:].split()] # on met les nombres dans une liste L recréée
chout += line # et on enregistre la ligne START dans chout
elif line[0:4] == 'BEND': # lignes BEND quand il y en a
L.append(line[14:].split()) # on ajoute seulement la ligne à L
elif line[0:6]=='FINISH': # ligne FINISH
# on va faire les calculs sur les lignes BEND et FINISH avant d'enregistrer
L.append(line[14:].split()) # ajout nécessaire de la denière ligne à L
for i in xrange(1,len(L)-1): # calculs et enregistrement dans chout des lignes BEND
li = [str(float(L[i][k])-float(L[i-1][k])) for k in xrange(5)]
chout += ('BEND' + str(i)).ljust(14)\
+ ''.join(x.rjust(9) for x in li) + '\n'
chout += 'FINISH '\
+ ''.join(str(float(L[-1][k])-float(L[-2][k])).rjust(9)
for k in xrange(5)) + '\n' # ajout de FINISH
else:
chout += line # on enregistre les lignes ND, OD, WALL-THICKNESS
# on enregistre la portion traitée dans un fichier de nom out_name
tf = clock()
A.append(tf-te)
### Code 2
#==========
# 2 expressions regulieres, L = liste de nombres sous forme de chaine
# enregistrement des lignes BEND au fur et a mesure
te = clock()
for y in xrange(repet):
with open('piktux.txt','r') as f:
ch = f.read()
patSpool = re.compile('/SPOOL\r?\nSPOOL-ID\s+(\d+)\r?\nSPOOL-PREFIX\s+(.+?)\r?\n'
'(.+?)/END-SPOOL',re.DOTALL)
agarder = ('^ND {23}', '^OD {23}', '^WALL-THICKNESS {11}',
'^START {9}','^BEND[\d ]{10}','^FINISH {8}')
# dans agarder: en-têtes précis des lignes à garder
patLigne = re.compile( '(' + '|'.join( ag+'.+\n' for ag in agarder ) + ')',re.MULTILINE )
for id,prefix,portion in (m.groups() for m in patSpool.finditer(ch)):
# on crée le nom du fichier qui va recevoir le contenu de cette portion
out_name = id + '^' + prefix + '.txt'
# on crée la sélection de lignes de cette portion qui va être enregistrée
chout = ''
for line in ( m.group() for m in patLigne.finditer(portion)):
if line[0:5] == 'START': # ligne START
b = 0
LP = line[14:].split() # on met les nombres dans une liste L recréée
chout += line
elif line[0:4] == 'BEND': # lignes BEND quand il y en a
b += 1
L = line[14:].split()
chout += ('BEND' + str(b)).ljust(14)\
+ ''.join(str(float(u)-float(LP[i])).rjust(9) for i,u in enumerate(L)) + '\n'
LP = L
elif line[0:6]=='FINISH': # ligne FINISH
chout += 'FINISH '\
+ ''.join(str(float(x)-float(LP[i])).rjust(9)
for i,x in enumerate(line[14:].split())) + '\n'
else:
chout += line # on enregistre les lignes ND, OD, WALL-THICKNESS
# on enregistre la portion traitée dans un fichier de nom out_name
#with open(out_name, 'w') as g:
#g.write(chout)
tf = clock()
B.append(tf-te)
#### Code 3
#==========
# 2 expressions regulieres, L = liste de floats grace a map()
# enregistrement des lignes BEND au fur et a mesure
te = clock()
for y in xrange(repet):
with open('piktux.txt','r') as f:
ch = f.read()
patSpool = re.compile('/SPOOL\r?\nSPOOL-ID\s+(\d+)\r?\nSPOOL-PREFIX\s+(.+?)\r?\n'
'(.+?)/END-SPOOL',re.DOTALL)
agarder = ('^ND {23}', '^OD {23}', '^WALL-THICKNESS {11}',
'^START {9}','^BEND[\d ]{10}','^FINISH {8}')
# dans agarder: en-têtes précis des lignes à garder
patLigne = re.compile( '(' + '|'.join( ag+'.+\n' for ag in agarder ) + ')',re.MULTILINE )
for id,prefix,portion in (m.groups() for m in patSpool.finditer(ch)):
# on crée le nom du fichier qui va recevoir le contenu de cette portion
out_name = id + '^' + prefix + '.txt'
# on crée la sélection de lignes de cette portion qui va être enregistrée
chout = ''
for line in ( m.group() for m in patLigne.finditer(portion)):
if line[0:5] == 'START': # ligne START
b = 0
LP = map(float,line[14:].split()) # on met les nombres dans une liste L recréée
chout += line
elif line[0:4] == 'BEND': # lignes BEND quand il y en a
b += 1
L = map(float,line[14:].split())
chout += ('BEND' + str(b)).ljust(14)\
+ ''.join(str(u-LP[i]).rjust(9) for i,u in enumerate(L)) + '\n'
LP = L
elif line[0:6]=='FINISH': # ligne FINISH
chout += 'FINISH '\
+ ''.join(str(x-LP[i]).rjust(9)
for i,x in enumerate(map(float,line[14:].split()))) + '\n'
else:
chout += line # on enregistre les lignes ND, OD, WALL-THICKNESS
# on enregistre la portion traitée dans un fichier de nom out_name
#with open(out_name, 'w') as g:
#g.write(chout)
tf = clock()
C.append(tf-te)
### Code 4
#=========
# pas d'expressions regulieres, L = liste de nombres sous forme de chaine
#del patLigne,patSpool
te = clock()
for y in xrange(repet):
f = open('piktux.txt', 'r')
agarder = ('ND ', 'OD ', 'WALL-THICKNESS')
# dans agarder: en-têtes précis des lignes à garder
for line in f:
if line[0:9]=='SPOOL-ID ':
id = line.split()[1]
elif line[0:13]=='SPOOL-PREFIX ':
out_name = id + '^' + line.split()[1] + '.txt'
chout = ''
# on crée la sélection de lignes de la portion qui va être enregistrée
elif line[0:6] == 'START ': # ligne START
b = 0
LP = line[14:].split() # on met les nombres dans une liste L recréée
chout += line # et on enregistre la ligne START dans chout
elif line[0:4] == 'BEND': # lignes BEND quand il y en a
b += 1
L = line[14:].split()
chout += ('BEND' + str(b)).ljust(14)\
+ ''.join(str(float(u)-float(LP[i])).rjust(9) for i,u in enumerate(L)) + '\n'
LP = L
elif line[0:7]=='FINISH ': # ligne FINISH
chout += 'FINISH '\
+ ''.join(str(float(x)-float(LP[i])).rjust(9)
for i,x in enumerate(line[14:].split())) + '\n'
elif line[0:10]=='/END-SPOOL':
pass
elif line[0:14] in agarder:
chout += line # on enregistre les lignes ND, OD, WALL-THICKNESS
tf = clock()
f.close()
temps = tf-te
D.append(temps)
for x in xrange(0):# v in (A,B,C,D,E,F,G,H):
print '\v avant 5'
print v,min(v),'len =',len(v)
### Code 5
#=========
# pas d'expressions regulieres, L = liste de floats
te = clock()
for y in xrange(repet):
f = open('piktux.txt', 'r')
agarder = ('ND ', 'OD ', 'WALL-THICKNESS')
# dans agarder: en-têtes précis des lignes à garder
for line in f:
if line[0:9]=='SPOOL-ID ':
id = line.split()[1]
elif line[0:13]=='SPOOL-PREFIX ':
out_name = id + '^' + line.split()[1] + '.txt'
chout = ''
# on crée la sélection de lignes de la portion qui va être enregistrée
elif line[0:6] == 'START ': # ligne START
b = 0
LP = map(float,line[14:].split()) # on met les nombres dans une liste L recréée
chout += line # et on enregistre la ligne START dans chout
elif line[0:4] == 'BEND': # lignes BEND quand il y en a
b += 1
L = map(float,line[14:].split())
chout += ('BEND' + str(b)).ljust(14)\
+ ''.join(str(u-LP[i]).rjust(9) for i,u in enumerate(L)) + '\n'
LP = L
elif line[0:7]=='FINISH ': # ligne FINISH
chout += 'FINISH '\
+ ''.join(str(x-LP[i]).rjust(9)
for i,x in enumerate(map(float,line[14:].split()))) + '\n'
elif line[0:10]=='/END-SPOOL':
pass
elif line[0:14] in agarder:
chout += line # on enregistre les lignes ND, OD, WALL-THICKNESS
tf = clock()
f.close()
temps = tf-te
E.append(temps)
for x in xrange(0):#v in (A,B,C,D,E,F,G,H):
print '\v apres 5'
print v,min(v),'len =',len(v)
print
###############################################################################
### Code 6
#=========
# Code de josmiley
t3 = clock()
for y in xrange(repet):
f = open('piktux.txt', 'r')
def spool(fichier) :
fname,out,bendout = '',[],[]
for l in open(fichier, 'r').readlines() :
try : bl = l.split()[0]
except : continue
if bl in ('/SPOOL', 'ND', 'OD', 'WALL-THICKNESS') : out.append(l)
elif bl == 'START' :
out.append(l)
bendout = l.split()[1:]
elif bl.startswith('BEND') or bl == 'FINISH' :
out.append(''.join([bl.ljust(14)]+[str(float(i[1])-float(i[0])).rjust(9)
for i in zip(bendout,l.split()[1:])])+'\n')
bendout = l.split()[1:]
elif bl in ('SPOOL-ID', 'SPOOL-PREFIX') :
out.append(l)
fname = l.split()[1] + fname
elif bl == '/END-SPOOL' :
out.append(l)
#with open(fname+'.txt','w') as outfile :
#outfile.write(''.join(out))
fname,out,bendout = '',[],[]
spool('piktux.txt')
t4 = clock()
temps = t4-t3
F.append(temps)
### Code 7
#=========
# Code de josmiley modifie par eyquem
t7 = clock()
for y in xrange(repet):
def spool(fichier) :
fname,out,bendout = '',[],[]
for l in open(fichier, 'r').readlines() :
bl = l.split()
if not bl:
break
bl0 = bl[0]
if bl0 in ('/SPOOL', 'ND', 'OD', 'WALL-THICKNESS'):
out.append(l)
elif bl0 == 'START':
out.append(l)
bendout = bl[1:]
elif bl0[0:4]=='BEND' or bl0 == 'FINISH':
out.append(bl0.ljust(14)+''.join(str(float(act)-float(prec)).rjust(9)
for act,prec in zip(bl[1:],bendout))+'\n')
bendout = bl[1:]
elif bl0 in ('SPOOL-ID', 'SPOOL-PREFIX'):
out.append(l)
fname = bl[1] + fname
elif bl0 == '/END-SPOOL':
out.append(l)
#with open('zz'+fname+'.txt','w') as outfile:
#outfile.write(''.join(out))
fname,out,bendout = '',[],[]
spool('piktux.txt')
t8 = clock()
temps = t8-t7
G.append(temps)
### Code 8
#=========
# Code de josmiley modifie par eyquem 2
tj1 = clock()
for y in xrange(repet):
def spool(fichier) :
fname,out,bendout = '',[],[]
for l in open(fichier, 'r').readlines() :
bl = l.split()
if not bl:
break
bl0 = bl[0]
if bl0 in ('/SPOOL', 'ND', 'OD', 'WALL-THICKNESS'):
out.append(l)
elif bl0 == 'START':
out.append(l)
bendout = bl
elif bl0[0:4]=='BEND' or bl0 == 'FINISH':
out.append(bl0.ljust(14)+''.join(str(float(bl[k])-float(bendout[k])).rjust(9)
for k in xrange(1,6))+'\n')
bendout = bl
elif bl0 in ('SPOOL-ID', 'SPOOL-PREFIX'):
out.append(l)
fname = bl[1] + fname
elif bl0 == '/END-SPOOL':
out.append(l)
#with open('zz'+fname+'.txt','w') as outfile:
#outfile.write(''.join(out))
fname,out,bendout = '',[],[]
spool('piktux.txt')
tj2 = clock()
temps = tj2-tj1
H.append(tj2-tj1)
### Code 9
#=========
# pas d'expressions regulieres, L = liste de floats
te = clock()
for y in xrange(100):
f = open('piktux.txt', 'r')
agarder = ('ND ', 'OD ', 'WALL-THICKNESS')
# dans agarder: en-têtes précis des lignes à garder
for line in f:
if line[0:9]=='SPOOL-ID ':
id = line.split()[1]
elif line[0:13]=='SPOOL-PREFIX ':
out_name = id + '^' + line.split()[1] + '.txt'
chout = ''
# on crée la sélection de lignes de la portion qui va être enregistrée
elif line[0:6] == 'START ': # ligne START
L = map(float,line[14:].split()) # on met les nombres dans une liste L recréée
chout += line # et on enregistre la ligne START dans chout
elif line[0:4] == 'BEND' or line[0:7]=='FINISH ': # lignes BEND et FINISH
L.extend(map(float,line[14:].split()))
chout += line[14:]\
+ ''.join(str(L[k]-L[k-5]).rjust(9) for k in xrange(-5,0)) + '\n'
elif line[0:10]=='/END-SPOOL':
pass
#g = open('zUh'+out_name,'w')
#g.write(chout)
#g.close()
elif line[0:14] in agarder:
chout += line # on enregistre les lignes ND, OD, WALL-THICKNESS
tf = clock()
f.close()
temps = tf-te
I.append(temps)
print
print '==============='
for j,v in enumerate([A,B,C,D,E,F,G,H,I]):
if j+1==6:
print str(j+1).rjust(3),min(v),'len =',len(v),'code de josmiley'
elif j+1==9:
print '\n'+str(j+1).rjust(3),min(v),'len =',len(v),'BEND or FINISH '
else:
print str(j+1).rjust(3),min(v),'len =',len(v)
print '-- Fin du programme --' |
Partager