ohgreat Posted October 4, 2005 Share Posted October 4, 2005 (edited) Any ideas on how we can make a better mousetrap, er, stringrepeat routine? edit: Added routine as attachment edit2: Updated routine with better parameter checking and renamed to _StringRepeatFast(...)StringRepeatFast.au3StringRepeatFast_test.au3 Edited October 13, 2005 by ohgreat Link to comment Share on other sites More sharing options...
FuryCell Posted October 13, 2005 Share Posted October 13, 2005 (edited) Cool. Just ran a script to test the speeds and got these results_StringDup()=1.55438749896984_StringRepeat()=737.842658773671That means your UDF is more than 474 times faster than _StringRepeat. Nice Job. P.S. Here is the script in case anybody wants to test it for themselves.expandcollapse popup#Include<String.au3> $Timer=TimerInit() $X1=_StringDup("AAAA",4000) $CT1=TimerDiff($Timer) $Timer=TimerInit() $X1=_StringRepeat("AAAA",4000) $CT2=TimerDiff($Timer) MsgBox(0,"Done","_StringDup()=" & $CT1 & @LF & "_StringRepeat()=" & $CT2)[/size] [size=3][/size] [size=3] ;=============================================================================== ; ; Description: Fast string duplication routine. ; Parameter(s): $str - String to duplicate. ; $cnt - Number of times to duplicate. ; Requirement(s): None ; Return Value(s): On Success - Returns duplicated string (or "" if $cnt<= 0) ; On Failure - "" and sets @ERROR = 1 ; Author(s): Will Mooar ; Note(s): None ; ;=============================================================================== func _StringDup($str, $cnt) if $cnt <= 0 then Return "" Local $final_size = StringLen($str) * $cnt local $ret = $str ;surprisingly, this speeds things up alot (you wouldn't think the simple loop below would be so slow) if $cnt >= 16 Then $ret &= $ret $ret &= $ret $ret &= $ret $ret &= $ret endif ;keep doubling string size until we have built at least half of the final string Local $cur_size = StringLen($ret) while ($cur_size + $cur_size) <= $final_size $ret &= $ret $cur_size += $cur_size WEnd ;add the rest by taking a copy of as much of the first half as we need if $cur_size < $final_size then $ret &= StringLeft($ret, $final_size-$cur_size) endif return($ret) EndFunc Edited October 13, 2005 by SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code. Link to comment Share on other sites More sharing options...
ohgreat Posted October 13, 2005 Author Share Posted October 13, 2005 (edited) Cool. Just ran a script to test the speeds and got these resultsThat means your UDF is more than 474 times faster than _StringRepeat. Nice Job. Nice! Thanks for doing that! Hmm, I think I can live with "only" 474 x faster .. ps. Have updated UDF slightly (see 1st post) .. no speed improvements, but better parameter checking. Edited October 13, 2005 by ohgreat Link to comment Share on other sites More sharing options...
FuryCell Posted October 14, 2005 Share Posted October 14, 2005 I think you should look in trying to get this added to the standard UDF libary so it comes withs autoit. I don't use _StringRepeat much but IMO the speed increase warrants the replacement of the old _StringRepeat with _StringDup().If you do want to try then to get it added then post here. HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code. 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