wraithdu Posted December 13, 2008 Share Posted December 13, 2008 (edited) I recently had to do this type of replacement in a script of mine, and I was wondering which method was faster. Well, now I know expandcollapse popup$text = "[DefaultBox]" $timer = TimerInit() For $i = 1 To 100000 $new = StringTrimLeft($text, 1) $new = StringTrimRight($new, 1) Next ConsoleWrite("StringTrim* Time: " & TimerDiff($timer) / 1000 & " sec" & @CRLF) ConsoleWrite("Result: " & $new & @CRLF & @CRLF) $timer = TimerInit() For $i = 1 To 100000 $new = StringReplace($text, "[", "") $new = StringReplace($new, "]", "") Next ConsoleWrite("StringReplace Time: " & TimerDiff($timer) / 1000 & " sec" & @CRLF) ConsoleWrite("Result: " & $new & @CRLF & @CRLF) $timer = TimerInit() For $i = 1 To 100000 $new = StringRegExp($text, "\[(.*)\]", 1) $new = $new[0] Next ConsoleWrite("StringRegExp: " & TimerDiff($timer) / 1000 & " sec" & @CRLF) ConsoleWrite("Result: " & $new & @CRLF & @CRLF) $timer = TimerInit() For $i = 1 To 100000 $new = StringRegExpReplace($text, "\[(.*)\]", "\1") Next ConsoleWrite("StringRegExpReplace1: " & TimerDiff($timer) / 1000 & " sec" & @CRLF) ConsoleWrite("Result: " & $new & @CRLF & @CRLF) $timer = TimerInit() For $i = 1 To 100000 $new = StringRegExpReplace($text, "[][]", "") Next ConsoleWrite("StringRegExpReplace2: " & TimerDiff($timer) / 1000 & " sec" & @CRLF) ConsoleWrite("Result: " & $new & @CRLF) Results - StringTrim* Time: 0.625779812797437 sec Result: DefaultBox StringReplace Time: 1.83705324280041 sec Result: DefaultBox StringRegExp: 1.20775007717461 sec Result: DefaultBox StringRegExpReplace1: 1.300478622283 sec Result: DefaultBox StringRegExpReplace2: 1.25304235594189 sec Result: DefaultBox Edited December 13, 2008 by wraithdu Link to comment Share on other sites More sharing options...
wraithdu Posted December 13, 2008 Author Share Posted December 13, 2008 (edited) Wow, throw in a longer string, and the results are really interesting. $text = "[The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.]" StringTrim* Time: 0.625528034987687 sec Result: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. StringReplace Time: 7.80658268020098 sec Result: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. StringRegExp: 1.39551542165275 sec Result: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. StringRegExpReplace1: 1.48704514121208 sec Result: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. StringRegExpReplace2: 1.97352812997183 sec Result: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. Edited December 13, 2008 by wraithdu Link to comment Share on other sites More sharing options...
karman Posted December 13, 2008 Share Posted December 13, 2008 (edited) StringTrim* Time: 0.356628565921088 sec Result: DefaultBox StringReplace Time: 0.677000847873124 sec Result: DefaultBox StringRegExp: 0.70752986762284 sec Result: DefaultBox StringRegExpReplace1: 0.737454341264043 sec Result: DefaultBox StringRegExpReplace2: 0.774421330085248 sec Result: DefaultBox hm.. Edited December 13, 2008 by karman Link to comment Share on other sites More sharing options...
-Ultima- Posted December 13, 2008 Share Posted December 13, 2008 (edited) I don't think you can legitimately call StringTrim*() string replacement functions. They have very specific purposes, and don't require scanning of the entire string to figure out what stays and what gets replaced (they simply copy the part of the string that falls outside of the trimmed section), so naturally, they'd be faster.Basically, this test is highly dependent on the input, and isn't really a fair comparison of speed in the general sense. If you replaced "[DefaultBox]" with "([DefaultBox])", the tests suddenly return different strings -- definitely not something you want in a speed benchmark. Otherwise, I could just create a new function that simply returned "DefaultBox" on any given input, and it would be faster than your first test case -- doesn't mean it works well for all input, but it's still faster But yes, for your specific script, I suppose StringTrim*() would be the faster solution (you only seem to want to remove the first and last characters, after all).FYI, StringTrimRight(StringTrimLeft($text, 1), 1) is faster still Edited December 13, 2008 by -Ultima- [ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ] Link to comment Share on other sites More sharing options...
wraithdu Posted December 13, 2008 Author Share Posted December 13, 2008 (edited) Yes, this test was specific to the replacement I was doing. Of course if you change the input, you'll have to adjust the functions accordingly. The [DefaultBox] string was just a prototype. I was having to remove brackets from a list of returned strings. Edited December 13, 2008 by wraithdu Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now