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