Public Function Repeat_MTC(Extends src As String, repetitions As Integer) As String
// Repeats the given string repetitions times.
// Common cases
if repetitions < 1 then return ""
if repetitions = 1 then return src
if repetitions = 2 then return src + src
if repetitions = 3 then return src + src + src
dim curLenB as integer = src.LenB
dim targetLenB as integer = curLenB * repetitions
dim halfLenB as integer = ( targetLenB + 1 ) \ 2
while curLenB < halfLenB
src = src + src
curLenB = curLenB + curLenB
wend
dim diffB as integer = targetLenB - curLenB
if diffB <> 0 then src = src + LeftB( src, diffB )
return src
End Function
Yeah, this method takes the most of the time with allocating and deallocating memory
Did you actually test that method?
Kem, you have both, you want to know, test it and show us.
Not sure how that was helpful. In fact, I’m sure it wasn’t.
I didn’t incentivize you to test it and get the numbers? I’m sorry for my lack of proper conversation skills. I’m really not good at it.
Heads up, guys, Kem’s method is actually faster in higher repeat times, from about 200+ times
Kem, you knew that, didn’t you?
OK, now we have a challenge
Way back when, I was just as surprised as you seem to be now. It’s the effect of doubling and re-doubling the string. (Again, this was Joe Strout’s code, and I’m guessing he did the same sorts of tests.)
There is a method that may be available in an upcoming version of Xojo that could help make it faster. I’m going to test that soon.
As I wrote, it’s much faster than “my method” the higher the number of repetitions becomes (from about ca. 200). It takes only 16 iterations of concatenation plus a final one for 100,000 repetitions.
So, these types of discussion are great to learn something new. Thanks, Kem!
Moral of the story: Don’t question Kem.
LOL.
But I prefer it that way, because now I understand why and how!
Exponential approximation of the size, the inverse of logarithmic search looking for a value
.
I just tested. Repeating the letter “a” 10,000 times, and doing that 100 times, using the method in M_String vs. the potential new option yields these results in a compiled app:
M_String: 637 microsecs
Mystery method: 321 microsecs
M_String: 410 microsecs
Mystery method: 323 microsecs
M_String: 497 microsecs
Mystery method: 302 microsecs
Yes i’ve tested all versions by now and kem’s is way faster, wonder how xojo optimises internal code.
Some results:
As you can see, kem’s repeat wins by alot.
Time is in microseconds
Results on Mac M1 ARM64 native build
Nevermind this has a bug…
Public Function RepeatStr(str As String, numTimes As Integer) As String
[…]
End Function
Actually, all I expected from my original post was just “Yes, it’s this…”, or “No”. The discussion has been interesting, but now I’m confused:
“Function” only seems to occur in the documentation as part of XojoScript. So is the above intended to be XojoScript? Or is a just shorthand for the standard elements of the IDE (which of course are hard represent with just keyboard characters)?
For normal Xojo code in the IDE, you don’t put in the Function/End Function lines. The IDE does it for you. You put the function Name, Parameters, Return Type in the inspector fields for the function. If you were to copy the method from the navigator pane and past it into a text editor, you would see it in the format Rick posted. Or if you opened your text format project in a text editor, you would also see those lines.
This form is commonly used in the forum to show the full definition of a method/function. You wouldn’t paste it wholesale into the IDE. You would parse it out into its separate fields.