Salut les filles,
Je ne comprends pas bien le pourquoi de la boucle for i = 1 To 100000
Perso en passant comme suit (fonction no_for tout en bas), le timer n'est plus assez précis pour indiquer le temps d’exécution et rend 0 seconde.
Alors que les 2 autres solutions durent respectivement 0.04 secondes et 0.31 secondes...
Quelqu'un peu m'expliquer ce que vous avez cherché à faire avec cette For ?
Option Explicit
Private Type retour
chaine As String
duree As Double
End Type
Private Sub CommandButton1_Click()
Dim toto As String
toto = "Démonstration des différences de rapidité entre une méthode lourdaute et une méthode plus " & _
"fine (voila ce qui est entre les parenthèses). La différence saute aux yeux, non ?"
MsgBox voila(toto).duree & " secondes pour extraire """ & voila(toto).chaine & """ avec mon code"
MsgBox ExtraireTexte(toto).duree & " secondes pour extraire """ & ExtraireTexte(toto).chaine & """ avec le tien"
MsgBox no_for(toto).duree & " secondes pour extraire """ & voila(toto).chaine & """ sans utiliser la boucle For"
End Sub
Private Function ExtraireTexte(ByVal Texte As String) As retour
Dim deb As Double, i As Long, Rep1 As Integer, Rep2 As Integer
deb = Timer
For i = 1 To 100000
Rep1 = InStr(1, Texte, "(", vbTextCompare)
If Rep1 > 0 Then
Rep2 = InStr(1, Texte, ")", vbTextCompare)
If Rep2 > Rep1 Then
ExtraireTexte.chaine = Mid(Texte, Rep1 + 1, Rep2 - Rep1 - 1)
End If
End If
Next
ExtraireTexte.duree = Timer - deb
End Function
Private Function voila(ByVal Texte As String) As retour
Dim deb As Double, i As Long, Rep1 As Integer
deb = Timer
For i = 1 To 100000
Rep1 = InStr(1, Texte, "(")
If Rep1 Then
Texte = Mid(Texte, Rep1)
Rep1 = InStr(2, Texte, ")")
If Rep1 Then voila.chaine = Mid(Texte, 2, Rep1 - 2)
End If
Next
voila.duree = Timer - deb
End Function
Function no_for(ByVal Texte As String) As retour
Dim deb As Double
deb = Timer
Dim Rep1 As Integer
Dim Rep2 As Integer
Rep1 = InStr(1, Texte, "(") + 1
If Rep1 > 1 Then
Rep2 = InStr(Rep1, Texte, ")")
If Rep2 > 0 Then
no_for.chaine = Mid(Texte, Rep1, Rep2 - Rep1)
End If
End If
no_for.duree = Timer - deb
End Function