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
| def test1():
with open('outtest1.txt', 'w') as outfile:
with open('test.txt', 'r') as infile:
for l in infile.readlines():
outfile.write(l.split( )[0] + '\n')
def test2():
with open('outtest2.txt', 'w') as outfile:
with open('test.txt', 'r') as infile:
for l in infile:
outfile.write(l.split( )[0] + '\n')
def test3():
with open('outtest3.txt', 'w') as outfile:
with open('test.txt', 'r') as infile:
for l in iter(infile):
outfile.write(l.split( )[0] + '\n')
def test4():
open('outtest4.txt', 'w').write("\n".join([l.split( )[0] for l in
open('test.txt', 'r')]))
import time
def testfunc(func, t):
print('test de ' + func.__name__ + ':')
r = []
for t in range(t):
t1 = time.clock()
func()
t2 = time.clock()
r.append(t2-t1)
return sum(r)/len(r)
print('readlines() ' + str(testfunc(test1, 10)))
print('for l in infile ' + str(testfunc(test2, 10)))
print('iter(infile) ' + str(testfunc(test3, 10)))
print('oneshoot ;) ' + str(testfunc(test4, 10))) |
Partager