Character replacement in strings in VB.NET -


how fast can replace characters in string?

so background on question this: have couple of applications communicate each other , clients' applications through sockets. these socket messages contain non-printable characters (e.g. chr(0)) need replaced predetermined string (e.g "{nul}"}, because socket messages kept in log file. on side note, not every log message need have characters replaced.

now started off on little adventure reading this msdn link found different post site.

the current method used...at beginning of day...was using stringbuilder check possible replacements such as...

    public function replacesb(byval p_message string) string       dim sb new system.text.stringbuilder(p_message)        sb.replace(chr(0), "{nul}")       sb.replace(chr(1), "{soh}")        return sb.tostring     end function 

now blog post points out leaving stringbuilder out , using string.replace yield faster results. (actually, using stringbuilder slowest method of doing day long.)

    p_message = p_message.replace(chr(0), "{nul}")     p_message = p_message.replace(chr(1), "{soh}") 

knowing not every message need go through process thought save time not have process messages left out. using regular expressions first searched string , determined if needed processed or not. same using string.replace, wash saving time of not processing strings, losing time checking them regular expressions.

then suggested try using arrays matched indexes old , new , use process messages. this...

private chrarray() char = {chr(0), chr(1)} private strarray() string = {"{nul}", "{soh}"}  public function testreplace(byval p_message string) string     dim integer      = 0 ((chrarray.length) - 1)         if p_message.contains(chrarray(i).tostring)             p_message = p_message.replace(chrarray(i), strarray(i))         end if     next      return p_message end function 

this far has been fastest way have found process these messages. have tried various other ways of going converting incoming string character array , comparing along trying loop through string rather chrarray.

so question is: can make faster yet? missing?

you might able squeeze out little more speed reducing lookups. take example this:

    if p_message.contains(chrarray(i).tostring) 

the .contains method o(n). in worst case, you're going traverse chars in entire string without finding anything, expect traverse @ least 1 time each character in array, o(nm) n length of string , m number of chars you're replacing.

you might little better performance doing following (my vb-fu rusty, has not been tested ;) ):

private function writetocharlist(s string, dest list(of char))     each c char in s         dest.add(c)     next end function  public function testreplace(byval p_message string) string     dim chars new list(of char)(p_message.length)      each c 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 traverse chars in p_message @ twice (once traversing, once when string constructor copies char array), making function o(n).


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -