Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character replacement in strings in VB.NET

How fast can I replace characters in a string?

So the background on this question is this: We have a couple of applications that communicate with each other and with clients' applications through sockets. These socket messages contain non-printable characters (e.g. chr(0)) which need to get replaced with a predetermined string (e.g "{Nul}"}, because the socket messages are kept in a log file. On a side note, not every log message will need to have characters replaced.

Now I started off on this little adventure reading from this MSDN link which I found from a different post from this site.

The current method we used...at the beginning of the day...was using StringBuilder to check for all the possible replacements such as...

    Public Function ReplaceSB(ByVal p_Message As String) As String
      Dim sb As New System.Text.StringBuilder(p_Message)

      sb.Replace(Chr(0), "{NUL}")
      sb.Replace(Chr(1), "{SOH}")

      Return sb.ToString
    End Function

Now as the blog post points out leaving StringBuilder out and using string.replace does yield faster results. (Actually, using StringBuilder was the slowest method of doing this all day long.)

    p_Message = p_Message.Replace(Chr(0), "{NUL}")
    p_Message = p_Message.Replace(Chr(1), "{SOH}")

Knowing that not every message would need to go through this process I thought it would save time to not have to process those messages that could be left out. So using regular expressions I first searched the string and then determined if it needed to be processed or not. This was about the same as using the string.replace, basically a wash from saving the time of not processing all the strings, but losing time from checking them all with regular expressions.

Then it was suggested to try using some arrays that matched up their indexes with the old and the new and use that to process the messages. So it would be something like this...

Private chrArray() As Char = {Chr(0), Chr(1)}
Private strArray() As String = {"{NUL}", "{SOH}"}

Public Function TestReplace(ByVal p_Message As String) As String
    Dim i As Integer

    For i = 0 To ((chrArray.Length) - 1)
        If p_Message.Contains(chrArray(i).ToString) Then
            p_Message = p_Message.Replace(chrArray(i), strArray(i))
        End If
    Next

    Return p_Message
End Function

This so far has been the fastest way I have found to process these messages. I have tried various other ways of going about this as well like converting the incoming string into a character array and comparing along with also trying to loop through the string rather than the chrArray.

So my question to all is: Can I make this faster yet? What am I missing?

like image 290
Tim Avatar asked Nov 30 '10 22:11

Tim


People also ask

What is a replace string?

Definition and Usage The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced.

How do I replace all in a string?

To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag. replaceAll() method is more straight forward.


1 Answers

You might be able to squeeze out a little more speed by reducing some lookups. Take for example this:

    If p_Message.Contains(chrArray(i).ToString) Then

The .Contains method is O(n). In the worst case, you're going to traverse all the chars in the entire string without finding anything, so you expect to traverse at least one time for each character in your array, so its O(nm) where n is the length of your string and m is the number of chars you're replacing.

You might get a little better performance doing the following (my VB-fu is rusty, has not been tested ;) ):

Private Function WriteToCharList(s as String, dest as List(Of Char))
    for each c as Char in s
        dest.Add(c)
    Next
End Function

Public Function TestReplace(ByVal p_Message As String) As String
    Dim chars as new List(Of Char)(p_Message.Length)

    For each c as Char in p_Message
        Select Case c
            Case Chr(0): WriteToCharList("{NUL}", chars)
            Case Chr(1): WriteToCharList("{SOH}", chars)
            Case Else: chars.Add(c);
        End Select
    Next

    Return New String(chars)
End Function

This will traverse chars in p_Message at most twice (once for traversing, once when the string constructor copies the char array), making this function O(n).

like image 52
Juliet Avatar answered Oct 11 '22 15:10

Juliet