Replace methode, Replace Function, Stringbuilder replace, Regex Replace, Split

C

Cor

Hi Newsgroup,

I have given an answer in this newsgroup about a "Replace".

There came an answer on that I did not understand, so I have done some
tests.

I got the idea that someone said, that the split method and the
regex.replace method was better than the string.replace method and replace
function. I did not believe that.

I have tested this in two ways: with iteration of small strings and with a
long string. (Because that I myself use often the Stringbuilder replace,
have I added that too).

My results where comparative in spended time (not very scientific done) and
the 1 as basis.

Small strings
VB replace 4
String replace 1
Stringbuilder replace 2
Split 6
Regex 25
Long string
VB replace 170
String replace 1
Stringbuilder 2
Split 160
Regex 16

Who will check if my test program is right and get the same results?

Cor

Public Module Main
Public Sub Main()
Dim max As Integer = 20000
Dim oldstring As String = "**item1%%**item2%%**item3%%"
Dim newstring As String
Dim myarray() As String
Dim sb As New System.Text.StringBuilder
Dim StartTick As Integer = Environment.TickCount
For i As Integer = 0 To max
Next
Dim rest As Integer = Environment.TickCount - StartTick
StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring = Replace(oldstring, "%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
_
& " " & newstring, "Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring = oldstring.Replace("%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "String.Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
sb = New System.Text.StringBuilder(oldstring)
newstring = sb.Replace("%%", "").ToString
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "Stringbuilder.Replace")
StartTick = Environment.TickCount
For i As Integer = 0 To max
myarray = Split(oldstring, "%%", , CompareMethod.Text)
newstring = String.Join("", myarray)

Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "Split")

StartTick = Environment.TickCount
For i As Integer = 0 To max
newstring =
System.Text.RegularExpressions.Regex.Replace(oldstring, "%%", "")
Next
Debug.WriteLine((Environment.TickCount - StartTick - rest).ToString
& _
" " & newstring, "Regex.Replace")
Debug.WriteLine("----------------- now with long
string -------------")
sb = New System.Text.StringBuilder("")
For i As Integer = 0 To max
sb.Append(oldstring)
Next
oldstring = sb.ToString
StartTick = Environment.TickCount
newstring = Replace(oldstring, "%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString _
& " " & newstring.Substring(0, 20), "Replace")
StartTick = Environment.TickCount
newstring = oldstring.Replace("%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "String.Replace")
StartTick = Environment.TickCount
sb = New System.Text.StringBuilder(oldstring)
newstring = sb.Replace("%%", "").ToString
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Stringbuilder.Replace")
StartTick = Environment.TickCount
myarray = Split(oldstring, "%%", , CompareMethod.Text)
newstring = String.Join("", myarray)
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Split")
StartTick = Environment.TickCount
newstring = System.Text.RegularExpressions.Regex.Replace(oldstring,
"%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Regex.Replace")
End Sub
 
E

EricJ

here you go

Replace: 62 **item1**item2**item3
String.Replace: 16 **item1**item2**item3
Stringbuilder.Replace: 16 **item1**item2**item3
Split: 46 **item1**item2**item3
Regex.Replace: 235 **item1**item2**item3
----------------- now with long string -------------
Replace: 1454 **item1**item2**item
String.Replace: 15 **item1**item2**item
Stringbuilder.Replace: 16 **item1**item2**item
Split: 1172 **item1**item2**item
Regex.Replace: 140 **item1**item2**item
 
E

EricJ

max 200000
Replace: 390 **item1**item2**item3
String.Replace: 125 **item1**item2**item3
Stringbuilder.Replace: 203 **item1**item2**item3
Split: 516 **item1**item2**item3
Regex.Replace: 2469 **item1**item2**item3
----------------- now with long string -------------
Replace: 209297 **item1**item2**item
String.Replace: 94 **item1**item2**item
Stringbuilder.Replace: 156 **item1**item2**item
Split: 179563 **item1**item2**item
Regex.Replace: 1375 **item1**item2**item
 
C

CJ Taylor

Here ya go
Replace: 31 **item1**item2**item3

String.Replace: 16 **item1**item2**item3

Stringbuilder.Replace: 15 **item1**item2**item3

Split: 63 **item1**item2**item3

Regex.Replace: 266 **item1**item2**item3

----------------- now with longstring -------------

Replace: 1250 **item1**item2**item

String.Replace: 16 **item1**item2**item

Stringbuilder.Replace: 15 **item1**item2**item

Split: 985 **item1**item2**item

Regex.Replace: 109 **item1**item2**item

The program '[5092] speedtest1.exe' has exited with code 0 (0x0).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top