Jump to content

StringRepeat - very fast (using memset)


Zedna
 Share

Recommended Posts

This function is very fast compared to standard _StringRepeat() when number of repeated chars is big.

In my tests this version is faster than original for > 50 chars.

Time needed for this new StringRepeat() is constant no matter how many chars you repeat (nCount)

so for big numbers of repeated characters it's MUCH FASTER (hundred times).

StringRepeat Function:

Func StringRepeat($sChar, $nCount)
    $tBuffer = DLLStructCreate("char[" & $nCount & "]")
    DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DLLStructGetPtr($tBuffer), "int", Asc($sChar), "int", $nCount)
    Return DLLStructGetData($tBuffer, 1)
EndFunc

Testing example for compare speed with standard _StringRepeat (try to change number of chars):

#include <String.au3>

$start = TimerInit()
_StringRepeat('a',1000)
ConsoleWrite(TimerDiff($start)& @CRLF)

$start = TimerInit()
StringRepeat('a',1000)
ConsoleWrite(TimerDiff($start)& @CRLF)

Func StringRepeat($sChar, $nCount)
    $tBuffer = DLLStructCreate("char[" & $nCount & "]")
    DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DLLStructGetPtr($tBuffer), "int", Asc($sChar), "int", $nCount)
    Return DLLStructGetData($tBuffer, 1)
EndFunc

EDIT:

There is one difference/limitation from original _StringRepeat(): This new StringRepeat() can repeat only one character, so it's not posiible to do: StringRepeat('abc',3) --> result is 'aaa'

Maybe I should change its name from StringRepeat() to CharRepeat()

EDIT2:

Here is MemSet in form of UDF:

Func MemSet($pDest, $nChar, $nCount)
    DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", $pDest, "int", $nChar, "int", $nCount)
    If @error Then Return SetError(1,0,False)
    Return True
EndFunc
Edited by Zedna
Link to comment
Share on other sites

Nice function!

Something for the giant WinAPIEx library.

Thanks for sharing.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Nice.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Nice trick, can be used to change part of the buffers too.

$nCount = 100
$sChar = "a"
$tBuffer = DllStructCreate("char[" & $nCount & "]")
DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DllStructGetPtr($tBuffer), "int", Asc($sChar), "int", $nCount)
ConsoleWrite(DllStructGetData($tBuffer, 1) & @CRLF)
DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DllStructGetPtr($tBuffer) + 5, "int", Asc("b"), "int", 10)
ConsoleWrite(DllStructGetData($tBuffer, 1) & @CRLF)
DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DllStructGetPtr($tBuffer) + 5, "int", Asc(" "), "int", 10)
ConsoleWrite(DllStructGetData($tBuffer, 1) & @CRLF)
DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DllStructGetPtr($tBuffer) + 50, "int", Asc(" "), "int", DllStructGetSize($tBuffer) - 50)
ConsoleWrite(DllStructGetData($tBuffer, 1) & @CRLF)
DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DllStructGetPtr($tBuffer) + 75, "int", Asc("z"), "int", DllStructGetSize($tBuffer) - 75)
ConsoleWrite(DllStructGetData($tBuffer, 1) & @CRLF)
Link to comment
Share on other sites

  • 4 months later...

Here is old Feature request for native AutoIt's function StringRepeat()

where Jon wasn't against adding it.

Due to the limitation this won't be replacing the currently implemented _StringRepeat.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Due to the limitation this won't be replacing the currently implemented _StringRepeat.

You can always do something like

$s = StringRepeat("abc", 10)
ConsoleWrite($s & @CRLF)
Exit

Func StringRepeat($sChar, $nCount, $msvcrt = "msvcrt.dll")
   $nLen = StringLen($sChar)
   $tBuffer = DLLStructCreate("char[" & ($nLen * $nCount) & "]")
   $aSplit = StringSplit($sChar, "")
   $pBase = DLLStructGetPtr($tBuffer)
   For $n = 1 to UBound($aSplit)-1
      DllCall($msvcrt, "ptr:cdecl", "memset", "ptr", $pBase + ($n - 1), "int", Asc($aSplit[$n]), "int", 1)
   Next
   For $n = 0 To $nCount - 1
      DllCall($msvcrt, "ptr:cdecl", "memcpy", "ptr", $pBase + ($n * $nLen), "ptr", $pBase, "int", $nLen)
   Next
   Return DLLStructGetData($tBuffer, 1)
EndFunc

but I have no clue if that's OK due to my limited C++ knowledge. I didn't speed test because I think it will be much slower.

Edited by Manadar
Link to comment
Share on other sites

Here is another StringRepeat example.

$s = _StringRepeatRE("abc", 10)
ConsoleWrite($s & @CRLF)

; [url="http://www.autoitscript.com/forum/index.php?showtopic=101594&view=findpost&p=722113"]http://www.autoitscript.com/forum/index.php?showtopic=101594&view=findpost&p=722113[/url]
Func _StringRepeatRE($sString, $iRepeatCount)
    Return StringRegExpReplace(StringFormat("%" & $iRepeatCount & "s", " "), ".", $sString)
EndFunc   ;==>_StringRepeatRE
Link to comment
Share on other sites

but I have no clue if that's OK due to my limited C++ knowledge. I didn't speed test because I think it will be much slower.

I highly doubt this. I will have a look at your changes and see if it's worth replacing with the current UDF. I will however be changing the function as it currently uses a Select...EndSelect for some reason.

Here is another StringRepeat example.

Thanks. Though StringFormat increases the execution time unfortunately.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Neither the production version nor this DLL version seem very fast.

This one-liner screams along for any size string but due to numeric limitations only works for 15 or less repeats ($nCount):

Return StringReplace(10 ^ $nCount - 1, "9", $sChar)

This is also just as fast but only handles as high an $nCount value as characters in the local constant:

Local $str = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
 Return StringReplace(StringLeft($str, $nCount), "@", $sChar)

IMHO, I don't see a need for a rarely-used UDF function that only replaces the following:

For $x = 1 To $iRepeatCount
    $sResult &= $sString
Next
Link to comment
Share on other sites

  • 2 months later...
  • Moderators

I was searching Google for something and it led me to this link.

After looking at it, and looking at guinness's concerns, I'd like to chime in.

1. IMO, there's no error for a function like this. The person(s) using the function are responsible for managing their parameters.

2. Anything using DllStruct-Create/GetData whatever, is going to be slower normally than a simple concatenation. The only time I could see this being different maybe, is if the strings are huge and memory starts to get eaten up, but AutoIt's memory management is pretty well setup, so I don't see that really happening. This is true for single Chars or 2+Chars.

3. StringFormat(); well, it's not meant for speed, that's for sure.

Long story short, the only way I can see it being faster:

1. For long strings, dllcall to dll that manages the data itself without having to manage struct usage inside AutoIt.

2. Get rid of error checking.

3. We can save 1 loop cycle doing it the below way.

Func _StringRepeat($sString, $nCount)

    If $sString = Chr(0) Then Return

    Local $sRStr = $sString
    For $i = 1 To Int($nCount) - 1
        $sRStr &= $sString
    Next

    Return $sRStr
EndFunc

Edit:

This may be faster even:

Func _StringRepeat($sString, $nCount)

    Local $iLen = StringLen($sString)
    If $iLen = 0 Then Return

    Local $iMax = $iLen * $nCount
    While StringLen($sString) < $iMax
        $sString &= $sString
    WEnd
    Return StringLeft($sString, $iMax)
EndFunc

Edit2:

The last one has my vote. After benchmarking each, it was the most consistent in time to finish ( loops tested from 1 to 500 repeats over and over ). At a certain point, it was never more than 3 or 4 milliseconds, while the others were 10, 20, 30+ milliseconds as the string grew (makes sense).

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I would say (in the benchmarks I did) that this is quicker >> #2172

; #FUNCTION# ====================================================================================================================
; Name ..........: _StringRepeat
; Description ...: Repeats a string a specified number of times.
; Syntax ........: _StringRepeat($sString, $iRepeatCount)
; Parameters ....: $sString             - String to repeat
;                  $iRepeatCount        - Number of times to repeat the string
; Return values .: Success - Returns string with specified number of repeats
;                  Failure - Returns an empty string and sets @error = 1
;                  |@error  - 0 = No error.
;                  |@error  - 1 = One of the parameters is invalid
; Author ........: Jeremy Landes <jlandes at landeserve dot com>
; Modified.......: guinness - Removed Select...EndSelect statement and replaced with an If...EndIf as well as optimised the code.
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _StringRepeat($sString, $iRepeatCount)
    If (Not StringIsInt($iRepeatCount)) Or StringLen($sString) < 1 Or $iRepeatCount <= 0 Then Return SetError(1, 0, "")
    Local $sResult = ""
    While $iRepeatCount > 1
        If BitAND($iRepeatCount, 1) Then $sResult &= $sString
        $sString &= $sString
        $iRepeatCount = BitShift($iRepeatCount, 1)
    WEnd
    $sString &= $sResult
    Return $sString
EndFunc   ;==>__StringRepeat

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • Moderators

I would say (in the benchmarks I did) that this is quicker >> #2172

I don't know how you benchmarked that, but that statement is not true.

Edit:

I just realized this was not a thread on this subject exactly, that it was a thread on using memset specifically for this, I will not hijack any further.

My apologies Zedna.

Edit2:

( Editing so not to bump )

Here's a fair benchmark test:

Global $gstr_in = "aBcDeF"
Global $gstr1, $gstr2
Global $gntimer1, $gntimer2
Global $gndiff1, $gndiff2
Global $gna1 = 0, $gna2 = 0
For $j = 1 To 10
    For $i = 1 To 500
        Switch Random(0, 9, 1)
            Case 0 To 4
                Sleep(10)
                    $gntimer1 = TimerInit()
                    $gstr1 = _guinness($gstr_in, $i)
                    $gndiff1 = TimerDiff($gntimer1)

                Sleep(10)
                    $gntimer2 = TimerInit()
                    $gstr2 = _SmOke_N($gstr_in, $i)
                    $gndiff2 = TimerDiff($gntimer2)
            Case Else
                Sleep(10)
                    $gntimer2 = TimerInit()
                    $gstr2 = _SmOke_N($gstr_in, $i)
                    $gndiff2 = TimerDiff($gntimer2)

                Sleep(10)
                    $gntimer1 = TimerInit()
                    $gstr1 = _guinness($gstr_in, $i)
                    $gndiff1 = TimerDiff($gntimer1)
            EndSwitch


        $gna1 += $gndiff1
        $gna2 += $gndiff2
    Next
Next
ConsoleWrite("Guinness:" & @TAB & $gna1 & @CRLF & "SmOke_N:" & @TAB & $gna2 & @CRLF)

Func _guinness($sString, $iRepeatCount)
    If (Not StringIsInt($iRepeatCount)) Or StringLen($sString) < 1 Or $iRepeatCount <= 0 Then Return SetError(1, 0, "")
    Local $sResult = ""
    While $iRepeatCount > 1
        If BitAND($iRepeatCount, 1) Then $sResult &= $sString
        $sString &= $sString
        $iRepeatCount = BitShift($iRepeatCount, 1)
    WEnd
    $sString &= $sResult
    Return $sString
EndFunc

Func _SmOke_N($sString, $iCount)

    Local $iLen = StringLen($sString)
    If $iLen = 0 Then Return

    Local $iMax = $iLen * Int($iCount)
    While StringLen($sString) < $iMax
        $sString &= $sString
    WEnd
    Return StringLeft($sString, $iMax)
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

@guinness

In this case Select / EndSelect is slower than If / EndIf?

Inline is not slower,

If BitAND($iRepeatCount, 1) Then $sResult &= $sString

than this way:?

If BitAND($iRepeatCount, 1) Then
    $sResult &= $sString
EndIf

JS

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
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
 Share

×
×
  • Create New...