By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,630 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,630 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(oldst ring, "%%", "")
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(oldst ring,
"%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Regex.Replace")
End Sub


Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"Cor" <no*@non.com> wrote in message
news:uM**************@tk2msftngp13.phx.gbl...
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(oldst ring, "%%", "")
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(oldst ring, "%%", "")
Debug.WriteLine((Environment.TickCount - StartTick).ToString & _
" " & newstring.Substring(0, 20), "Regex.Replace")
End Sub


Nov 20 '05 #2

P: n/a
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
"EricJ" <er********@ThiSbitconsult.be.RE> wrote in message
news:40**********************@news.skynet.be...
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

Nov 20 '05 #3

P: n/a
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).
Nov 20 '05 #4

P: n/a
Cor
Hi Cj,

Thanks,

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

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.