count occurrences of string within a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dana King

    count occurrences of string within a string

    I'm looking for some other developers opinions, I'm trying to find the best
    way to count strings within a string using VB.net.

    I have tested five methods and have found the String.Replace method is the
    fastest and the Regex.Matches.C ount to be the slowest. I posted my results
    and source code to my web site. If you could take a look and maybe suggest
    an even faster method I'd like to hear from you. Also, if anyone can tell me
    why Regex is so much slower than the rest I'd like to know about it.

    Thanks.

    http://www.dotnetmania c.com/ArticleViewer.a spx?Key={4d9141 de-2f82-4490-80ca-c9f725c4e291}



  • GhostInAK

    #2
    Re: count occurrences of string within a string

    Hello Dana,

    Looking for only a single character I can get about twice as much speed using
    a char loop.

    Using For Next and String.Equals.. .
    1's found: 499,898 in 5,000,000 characters.
    elpased time: 0.21875

    Using Do Loop with InStr function...
    1's found: 499,898 in 5,000,000 characters.
    elpased time: 0.25

    Using Do Loop with String.IndexOf. ..
    1's found: 499,898 in 5,000,000 characters.
    elpased time: 0.109375

    Using String.Replace. ..
    1's found: 499,898 in 5,000,000 characters.
    elpased time: 0.0625

    Using RegEx.Matches.C ount...
    1's found: 499,898 in 5,000,000 characters.
    elpased time: 0.828125

    Using Char Loop...
    1's found: 499,898 in 5,000,000 characters.
    elpased time: 0.015625

    snippet:
    '== Using char loop
    Private Sub test6(ByVal totalLength As Integer, ByVal fileContents As String)

    Dim one As Integer = 0
    Dim startTime As Int64 = Now.Ticks
    Dim tCount As Integer = 0

    For tCount = 0 To fileContents.Le ngth - 1
    If fileContents(tC ount) = "1"c Then
    one += 1
    End If
    Next
    Dim endTime As Long = Now.Ticks
    Dim elapsedTime As Double = TimeSpan.FromTi cks(endTime - startTime).Tota lSeconds
    Console.WriteLi ne("Using Char Loop...")
    Console.WriteLi ne("1's found: " & Format(one, "#,#") & " in " & Format(totalLen gth,
    "#,#") & " characters.")
    Console.WriteLi ne("elpased time: " & elapsedTime.ToS tring)
    Console.WriteLi ne()
    Console.WriteLi ne()
    End Sub


    -Boo
    I'm looking for some other developers opinions, I'm trying to find the
    best way to count strings within a string using VB.net.
    >
    I have tested five methods and have found the String.Replace method is
    the fastest and the Regex.Matches.C ount to be the slowest. I posted my
    results and source code to my web site. If you could take a look and
    maybe suggest an even faster method I'd like to hear from you. Also,
    if anyone can tell me why Regex is so much slower than the rest I'd
    like to know about it.
    >
    Thanks.
    >
    http://www.dotnetmania c.com/ArticleViewer.a spx?Key={4d9141 de-2f82-4490
    -80ca-c9f725c4e291}
    >

    Comment

    • Mudhead

      #3
      Re: count occurrences of string within a string

      In test1 try this:

      For i As Integer = 0 To totalLength - 1
      If fileContents(i) = "1"c Then one += 1
      Next


      In test3 make sure you specify a char (i.e."1"c) and not a string("1")

      Do : Result = fileContents.In dexOf("1"c, Start)
      If Result = -1 Then Exit Do
      one += 1 : Start = Result + 1 : Loop



      "Dana King" <bushido101@hot mail.comwrote in message
      news:e4JkXAYxGH A.5044@TK2MSFTN GP05.phx.gbl...
      I'm looking for some other developers opinions, I'm trying to find the
      best way to count strings within a string using VB.net.
      >
      I have tested five methods and have found the String.Replace method is the
      fastest and the Regex.Matches.C ount to be the slowest. I posted my results
      and source code to my web site. If you could take a look and maybe suggest
      an even faster method I'd like to hear from you. Also, if anyone can tell
      me why Regex is so much slower than the rest I'd like to know about it.
      >
      Thanks.
      >
      http://www.dotnetmania c.com/ArticleViewer.a spx?Key={4d9141 de-2f82-4490-80ca-c9f725c4e291}
      >
      >
      >

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: count occurrences of string within a string

        Dana,

        We have tested this some years ago, from my memory.

        For a string in a string is the best method to count to use the VB Net Instr
        while going forward in the string,

        For a single char in a string is the best method the string.indexoff ("x"c)
        doing the same.



        For sure I remember that with counting strings is the Instr twice as fast as
        the indexof. The regex and the split string are as far as I remember me
        about 100 times slower. Using the instr with a single char is extremely
        slower than the indexof with a char.

        I hope this helps,

        Cor



        "Dana King" <bushido101@hot mail.comschreef in bericht
        news:e4JkXAYxGH A.5044@TK2MSFTN GP05.phx.gbl...
        I'm looking for some other developers opinions, I'm trying to find the
        best way to count strings within a string using VB.net.
        >
        I have tested five methods and have found the String.Replace method is the
        fastest and the Regex.Matches.C ount to be the slowest. I posted my results
        and source code to my web site. If you could take a look and maybe suggest
        an even faster method I'd like to hear from you. Also, if anyone can tell
        me why Regex is so much slower than the rest I'd like to know about it.
        >
        Thanks.
        >
        http://www.dotnetmania c.com/ArticleViewer.a spx?Key={4d9141 de-2f82-4490-80ca-c9f725c4e291}
        >
        >
        >

        Comment

        Working...