vb.net character replacement in strings

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 client's 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 string builder 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 string builder out and using string.replace does yield faster results.(Actually using string builder 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 regex I first searched the string 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 regex.

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?

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).


Need Your Help

Git: force push cleared all of my commits - can I get them back?

git bitbucket git-commands

My live site was updated since my last commit, and I hadn't made any local changes, so I wanted to just push everything that's live up to my Git repo.

Communicating with Linux scripts from php web

java php linux sockets communication

I'm building a web application that helps people improve their English pronunciation for some words. The website displays a sentence for the user, and he/she speaks it and then press "Results" butt...