|
-
Jun 6th, 2010, 03:04 AM
#12
Re: Remove string is between "()" [help]
anhn: what testing code you used for speed? I don't find your code faster even under IDE (which is where my code is normally much slower).
IDE
RemoveParentheses2: 25.57747 ms
RemoveBetween2: 91.07186 ms
RemoveBetween1: 184.57736 ms
Compiled with advanced optimizations on
RemoveParentheses2: 16.21600 ms
RemoveBetween2: 37.44149 ms
RemoveBetween1: 81.12066 ms
Code:
Option Explicit
Private Sub Command1_Click()
Dim strTest As String, I As Long
Const TESTCOUNT = 10000
Const TESTSTRING = "Hi this is a test (which is nonsense), just to show you an example (lol)."
Const TESTFORMAT = "0.00000 ms"
Timing = 0
For I = 1 To TESTCOUNT
strTest = RemoveParentheses(TESTSTRING)
Next I
List1.AddItem "RemoveParentheses: " & Format$(Timing * 1000, TESTFORMAT)
Debug.Print """" & strTest & """"
Debug.Print List1.List(List1.NewIndex)
Timing = 0
For I = 1 To TESTCOUNT
strTest = RemoveParentheses2(TESTSTRING)
Next I
List1.AddItem "RemoveParentheses2: " & Format$(Timing * 1000, TESTFORMAT)
Debug.Print """" & strTest & """"
Debug.Print List1.List(List1.NewIndex)
Timing = 0
For I = 1 To TESTCOUNT
strTest = RemoveBetween1(TESTSTRING, "(", ")")
Next I
List1.AddItem "RemoveBetween1: " & Format$(Timing * 1000, TESTFORMAT)
Debug.Print """" & strTest & """"
Debug.Print List1.List(List1.NewIndex)
Timing = 0
For I = 1 To TESTCOUNT
strTest = RemoveBetween2(TESTSTRING, "(", ")")
Next I
List1.AddItem "RemoveBetween2: " & Format$(Timing * 1000, TESTFORMAT)
Debug.Print """" & strTest & """"
Debug.Print List1.List(List1.NewIndex)
List1.ListIndex = List1.NewIndex
End Sub
If the test string was bigger then the difference would be much greater, because your code relies in methods that require a lot of memory (first one: creation of new strings with Replace, Split, looping & Join... second one: Split, RTrim, string concatenation). The functions I provided both only create two new strings in a single call (ByVal Text, Left$... using Mid$ on both sides does not create a new string, it is a direct memory copy).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|