Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Optimization on the Occurs() method
Message
De
10/08/2006 00:19:41
 
 
À
09/08/2006 23:39:15
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 8.0
OS:
Windows XP SP2
Database:
Visual FoxPro
Divers
Thread ID:
01144405
Message ID:
01144416
Vues:
13
This message has been marked as the solution to the initial question of the thread.
If you're going to do string manipulation inside of loops, you should be using the StringBuilder instead.

Another option is one I found in the VFP ToolKit for .NET (I assume it works, I haven't tested it):
Public Shared Function Occurs(ByVal tcChar As Char, ByVal cExpression As String) As Integer
    Dim i As Integer, nOccured As Integer = 0
    'Loop through the string
    For i = 0 To cExpression.Length - 1 Step i + 1
        'Check if each expression is equal to the one we want to check against
        If cExpression.Chars(i) = tcChar Then
            'if  so increment the counter
            nOccured = nOccured + 1
        End If
    Next
    Return nOccured
End Function

Public Shared Function Occurs(ByVal cString As String, ByVal cExpression As String) As Integer
    Dim nPos As Integer = 0
    Dim nOccured As Integer = 0
    Do
        'Look for the search string in the expression
        nPos = cExpression.IndexOf(cString, nPos)
        If nPos < 0 Then
            'This means that we did not find the item
            Exit Do
        Else
            'Increment the occured counter based on the current mode we are in
            nOccured = nOccured + 1
            nPos = nPos + 1
        End If
    Loop While True
    'Return the number of occurences
    Return nOccured
End Function
~~Bonnie




>I have this VFP equivalent Occurs() method but it is extremely slow. I just found that if I call this method several times that is is slowing down my process. Anyone would know what could be done to optimize this?
>
>
>        ' Count the occurences of one string within another
>        ' expC1 Character to look for
>        ' expC2 String
>        Public Function Occurs(ByVal tcCharacter As String, ByVal tcString As String) As Integer
>            Dim lcString As String = ""
>            Dim lnCount As Integer = 0
>            Dim lnLocation As Integer = 0
>            lcString = tcString
>            lnLocation = lcString.IndexOf(tcCharacter)
>            Do While lnLocation <> -1
>                lnCount = lnCount + 1
>                lcString = Mid(lcString, lnLocation + 2)
>                lnLocation = lcString.IndexOf(tcCharacter)
>            Loop
>            Return lnCount
>        End Function
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform