Bonjour,

Envoyé par
Bktero
@tyrtamos : joli code !
Merci!
@manticore: je n'ai pas pu résister: je viens d'essayer mon code en Cython pour me rapprocher de ma proposition précédente: faire la boucle de comptage des temps en C. Mais c'est beaucoup moins douloureux en Cython pour moi qui n'ai pas codé en C depuis très longtemps...
J'ai donc placé la fonction estegal en Cython, ainsi que la boucle des temps appelés ici tempsexec, et j'ai appelé ce fichier test01bib.pyx:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from time import clock
####################################################
def estegal(char *mot):
cdef char *ref
ref = "machin" # mot de référence à trouver
if len(mot)!=len(ref):
return False
for i in xrange(0, len(mot)):
if mot[i]!=ref[i]:
return False
return True
####################################################
def tempsexec(char *mot, long nrep):
cdef float t, tps
t = 0
for k in xrange(0, nrep):
tps = clock()
estegal(mot)
t += clock()-tps
return t |
Le setup.py:
1 2 3 4 5 6 7 8 9 10 11
| from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("test01bib", ["test01bib.pyx"])]
setup(
name = 'bibliotheque',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
) |
Et la compilation par:
1 2
| python setup.py build_ext --inplace
pause |
Il faut, bien entendu, avoir en dispo: cython et un compilateur C (j'utilise Visual Studio sous Windows 7)
On a ensuite le code Python d'appel qui se distinguera du précédent par:
- l'importation de test01bib.pyd qu'on vient de compiler:
from test01bib import tempsexec, estegal
- le calcul du temps pour chacun des mots essayés:
t = tempsexec(mot, 1000000)
- et, bien sûr, le calcul par bruteforce pour le dernier caractère à trouver.
Voilà ce que ça donne:
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
| 1 a 0.221920147538
2 aa 0.231556817889
3 aaa 0.229974657297
4 aaaa 0.232136800885
5 aaaaa 0.229839041829
6 aaaaaa 0.499046057463
nb de caracteres: 6
aaaaaa 0.499537497759
baaaaa 0.500251531601
caaaaa 0.499537438154
daaaaa 0.499188274145
eaaaaa 0.501381516457
faaaaa 0.499670147896
gaaaaa 0.499998509884
haaaaa 0.499508976936
iaaaaa 0.499826908112
jaaaaa 0.500127375126
kaaaaa 0.501209974289
laaaaa 0.499835133553
maaaaa 0.547224760056
naaaaa 0.500816464424
oaaaaa 0.500645935535
paaaaa 0.500765562057
qaaaaa 0.499796271324
raaaaa 0.498915225267
saaaaa 0.499899566174
taaaaa 0.501038551331
uaaaaa 0.49939545989
vaaaaa 0.499789178371
waaaaa 0.501508891582
xaaaaa 0.499635607004
yaaaaa 0.501338481903
zaaaaa 0.499383330345
trouvé: m
maaaaa 0.546270549297
mbaaaa 0.533521711826
mcaaaa 0.53567481041
mdaaaa 0.534054636955
meaaaa 0.534450769424
mfaaaa 0.538763999939
mgaaaa 0.536012351513
mhaaaa 0.536052286625
miaaaa 0.534889280796
mjaaaa 0.534777164459
mkaaaa 0.535886526108
mlaaaa 0.535616755486
mmaaaa 0.533975601196
mnaaaa 0.53526365757
moaaaa 0.537335395813
mpaaaa 0.535381674767
mqaaaa 0.535434961319
mraaaa 0.538470089436
msaaaa 0.53471916914
mtaaaa 0.535782754421
muaaaa 0.537365078926
mvaaaa 0.535662829876
mwaaaa 0.538740158081
mxaaaa 0.535456180573
myaaaa 0.537270605564
mzaaaa 0.534474611282
trouvé: ma
maaaaa 0.544574677944
mabaaa 0.546063423157
macaaa 0.567969441414
madaaa 0.544914424419
maeaaa 0.545920610428
mafaaa 0.546736657619
magaaa 0.54584646225
mahaaa 0.546464145184
maiaaa 0.545074462891
majaaa 0.546313226223
makaaa 0.546055138111
malaaa 0.545982420444
mamaaa 0.546511948109
manaaa 0.545819163322
maoaaa 0.545042395592
mapaaa 0.546055316925
maqaaa 0.545487523079
maraaa 0.546147763729
masaaa 0.545496761799
mataaa 0.546200037003
mauaaa 0.545724511147
mavaaa 0.546180188656
mawaaa 0.548753261566
maxaaa 0.544449269772
mayaaa 0.54485720396
mazaaa 0.545593857765
trouvé: mac
macaaa 0.566424250603
macbaa 0.567747592926
maccaa 0.567854225636
macdaa 0.567887604237
maceaa 0.568170130253
macfaa 0.565382957458
macgaa 0.567666053772
machaa 0.592935144901
maciaa 0.568876326084
macjaa 0.566256165504
mackaa 0.567029178143
maclaa 0.567127645016
macmaa 0.567187666893
macnaa 0.568488061428
macoaa 0.566083490849
macpaa 0.56672167778
macqaa 0.567192971706
macraa 0.567043364048
macsaa 0.568328917027
mactaa 0.56847679615
macuaa 0.567109405994
macvaa 0.56794577837
macwaa 0.566846549511
macxaa 0.56706982851
macyaa 0.569503724575
maczaa 0.566954135895
trouvé: mach
machaa 0.592271625996
machba 0.593430995941
machca 0.590678215027
machda 0.593964934349
machea 0.592410802841
machfa 0.592570841312
machga 0.592744529247
machha 0.592476725578
machia 0.617188215256
machja 0.592975735664
machka 0.591249883175
machla 0.594406783581
machma 0.592617809772
machna 0.59311491251
machoa 0.591411173344
machpa 0.593977272511
machqa 0.589469015598
machra 0.591496646404
machsa 0.593277037144
machta 0.592966079712
machua 0.589692175388
machva 0.593700885773
machwa 0.593548893929
machxa 0.591104447842
machya 0.594294786453
machza 0.59226167202
trouvé: machi
trouvé: machin |
En ayant la fonction de test du mot "estegal" en C, on se rapproche du problème posé au début.
On note que le cumul des temps pour 1 million d'essais est 7 fois plus rapide environ. La dispersion des temps d'un essai à l'autre est plus réduit, mais comme le temps de réponse de la fonction estegal est aussi réduit, on n'est pas plus discriminant.
Cependant: ça marche!!! 
Ok?
Partager