Skip to content

Instantly share code, notes, and snippets.

@knjname
Created January 13, 2015 15:36
Show Gist options
  • Save knjname/95e6ae8dbc46e3f3fe12 to your computer and use it in GitHub Desktop.
Save knjname/95e6ae8dbc46e3f3fe12 to your computer and use it in GitHub Desktop.
Performance comparison between my repeatFunction and WorksheetFunction.Rept
' << RESULT >>
' ? repeatString("abc", 10)
' abcabcabcabcabcabcabcabcabcabc
'
' My repeat() 0.2460938
' WS.Rept() 1.547852
Function repeatString$(ByVal repeated$, ByVal count&)
If count < 1 Then Exit Function
Do While True
If (count And 1) = 1 Then
repeatString = repeatString & repeated
count = count - 1
End If
count = count / 2
If count = 0 Then Exit Function
repeated = repeated & repeated
Loop
End Function
Sub testRepeat()
Dim s, e
Dim i&
s = Timer
For i = 0 To 100000
repeatString "abcd", 1000
Next
e = Timer
Debug.Print "My repeat()", e - s
s = Timer
For i = 0 To 100000
WorksheetFunction.Rept "abcd", 1000
Next
e = Timer
Debug.Print "WS.Rept()", e - s
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment