Sign in to follow this  
Followers 0
ohgreat

_StringDup UDF

4 posts in this topic

#1 ·  Posted (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.au3

StringRepeatFast_test.au3

Edited by ohgreat

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Cool. ;) Just ran a script to test the speeds and got these results

_StringDup()=1.55438749896984

_StringRepeat()=737.842658773671

That means your UDF is more than 474 times faster than _StringRepeat. :P

Nice Job. :mad2:

P.S. Here is the script in case anybody wants to test it for themselves.

#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 by SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Cool. ;) Just ran a script to test the speeds and got these results

That means your UDF is more than 474 times faster than _StringRepeat. :P

Nice Job. :mad2:

Nice! Thanks for doing that! Hmm, I think I can live with "only" 474 x faster .. :oops:

ps. Have updated UDF slightly (see 1st post) .. no speed improvements, but better parameter checking.

Edited by ohgreat

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0