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
| #! /usr/bin/env/ python
# -*- coding: utf-8 -*-
#
#
from random import randint
import os
import time
import shutil
def testopen(nb, name1, name2, repcible):
for index in range(0, nb):
out = open(repcible + 'couple_%s_%d_%s_%d.fasta'% (name1, index, name2,
index), 'w')
src1 = open('%s_%d'% (name1, index), 'r')
src2 = open('%s_%d'% (name2, index), 'r')
out.write(src1.read() + src2.read())
src1.close()
src2.close()
out.close()
def testwithopen(nb, name1, name2, repcible):
for index in range(0, nb):
with open(repcible + 'couple_%s_%d_%s_%d.fasta'% (name1, index, name2,
index), 'w') as out:
with open('%s_%d'% (name1, index), 'r') as src1:
with open('%s_%d'% (name2, index), 'r') as src2:
out.write(src1.read() + src2.read())
def testopen1(nb, n1, n2, rc):
op = open
st = str
for i in range(0, nb):
out = op(rc + "couple_" + n1 + "_" + st(i) + '_' + n2 + "_" + st(i) +
".fasta", 'w')
src1 = op(n1 + '_' + st(i), 'r')
src2 = op(n2 + '_' + st(i), 'r')
out.write(src1.read() + src2.read())
src1.close()
src2.close()
out.close()
def testwithopen1(nb, n1, n2, rc):
op = open
st = str
for i in range(0, nb):
with op(rc + "couple_" + n1 + "_" + st(i) + '_' + n2 + "_" + st(i) +
".fasta", 'w') as out:
with op(n1 + '_' + st(i), 'r') as src1:
with op(n2 + '_' + st(i), 'r') as src2:
out.write(src1.read() + src2.read())
def testwithopen2(nb, n1, n2, rc):
op = open
st = str
i = 0
while i < nb:
with op(rc + "couple_" + n1 + "_" + st(i) + '_' + n2 + "_" + st(i) +
".fasta", 'w') as out:
with op(n1 + '_' + st(i), 'r') as src1:
with op(n2 + '_' + st(i), 'r') as src2:
out.write(src1.read() + src2.read())
i += 1
def testopen2(nb, n1, n2, rc):
op = open
st = str
i = 0
while i < nb:
out = op(rc + "couple_" + n1 + "_" + st(i) + '_' + n2 + "_" + st(i) +
".fasta", 'w')
src1 = op(n1 + '_' + st(i), 'r')
src2 = op(n2 + '_' + st(i), 'r')
out.write(src1.read() + src2.read())
src1.close()
src2.close()
out.close()
i += 1
def inittest(nb, tests):
print('Grand ménage...')
print('Création des répertoires cibles')
for name in tests:
if os.path.isdir(name):
shutil.rmtree(name)
os.mkdir(name)
print('Suppression des fichiers')
for item in os.listdir(os.getcwd()):
if item.startswith('src'):
os.remove(item)
print('Création de 2 x ' + str(nb) + ' fichiers')
letters = 'acgt'
for index in range(0, nb):
src1 = ''
src2 = ''
for i in range(1, 88):
src1 += letters[randint(0, 3)]
src2 += letters[randint(0, 3)]
with open('src1_' + str(index), 'w') as f1:
f1.write(src1 + '\n')
with open('src2_' + str(index), 'w') as f2:
f2.write(src2 + '\n')
if __name__ == "__main__":
nb = 100000
tests = ['testopen', 'testopen1', 'testwithopen', 'testwithopen1',
'testopen2', 'testwithopen2']
inittest(nb, tests)
print('Début des tests')
print('open:')
repcible = os.path.join(os.getcwd(), 'testopen') + os.sep
t1 = time.clock()
testopen(nb, 'src1', 'src2', repcible)
t2 = time.clock()
print(t2-t1)
print('with open:')
repcible = os.path.join(os.getcwd(), 'testwithopen') + os.sep
t1 = time.clock()
testwithopen(nb, 'src1', 'src2', repcible)
t2 = time.clock()
print(t2-t1)
# A partir d'ici on limite les LOAD_GLOBAL et remplace %s/%d par +
print('open1:')
repcible = os.path.join(os.getcwd(), 'testopen1') + os.sep
t1 = time.clock()
testopen1(nb, 'src1', 'src2', repcible)
t2 = time.clock()
print(t2-t1)
print('with open1:')
repcible = os.path.join(os.getcwd(), 'testwithopen1') + os.sep
t1 = time.clock()
testwithopen1(nb, 'src1', 'src2', repcible)
t2 = time.clock()
print(t2-t1)
# On remplace for par while
print('open2:')
repcible = os.path.join(os.getcwd(), 'testopen2') + os.sep
t1 = time.clock()
testopen2(nb, 'src1', 'src2', repcible)
t2 = time.clock()
print(t2-t1)
print('with open2:')
repcible = os.path.join(os.getcwd(), 'testwithopen2') + os.sep
t1 = time.clock()
testwithopen2(nb, 'src1', 'src2', repcible)
t2 = time.clock()
print(t2-t1) |
Partager