@Calcyman,
Once I understood how your function worked, I saw a method of making it a little faster by calculating the optimal value of 'filter' and looping until filter was empty.
function NewMultiplyString(char as string, num as integer)
`This method uses bitwise operations to achieve a faster speed.
filter = num || (num >> 1)
filter = filter || (filter >> 2)
filter = filter || (filter >> 4)
filter = filter || (filter >> 8)
filter = (filter || (filter >> 16)) + 1
while filter
a$ = a$ + a$
If (num && filter) then a$ = a$ + char
filter = filter >> 1
endwhile
endfunction a$
The stuff I added is a method of calculating the next higher power of 2 of 'num', then using that value for the filter.
I also wrote my own using my plug-in commands - on my system it's fastest until the number of duplications is approx 600, then the improved version of yours overtakes it again.
function ReplaceString(char as string, num as integer)
a$ = replace all$(padright$("", num), " ", char)
endfunction a$
Complete test code used:
Loops = 1000
Dups = 1000
sync on
sync rate 0
oldtime = timer()
For x = 1 to Loops
c$ = linearstr("Hello ",Dups)
Next x
print timer()-oldtime;" microseconds - linearstr"
print len(c$)
print ""
oldtime = timer()
For x = 1 to Loops
c$ = AddString("Hello ",Dups)
Next x
print timer()-oldtime;" microseconds - addstring"
print len(c$)
print ""
oldtime = timer()
For x = 1 to Loops
c$ = MultiplyString("Hello ",Dups)
Next x
print timer()-oldtime;" microseconds - multiplystring"
print len(c$)
print ""
oldtime = timer()
For x = 1 to Loops
c$ = NewMultiplyString("Hello ",Dups)
Next x
print timer()-oldtime;" microseconds - newmultiplystring"
print len(c$)
print ""
oldtime = timer()
For x = 1 to Loops
c$ = ReplaceString("Hello ",Dups)
Next x
print timer()-oldtime;" microseconds - replacestring"
print len(c$)
print ""
sync off
wait key
end
function linearstr(char as string, num as integer)
`This is the conventional method, which repeatedly appends the character to the end of the string.
For x = 1 to num
a$ = a$ + char
Next x
endfunction a$
function addString( char as string , num as integer )
while n <= (num-1)
inc n : a$ = a$ + char
endwhile
endfunction a$
function MultiplyString(char as string, num as integer)
`This method uses bitwise operations to achieve a faster speed.
filter = 67108864
For x = 0 to 26
a$ = a$ + a$
If (num && filter)>0 then a$ = a$ + char
filter = filter >> 1
Next x
endfunction a$
function NewMultiplyString(char as string, num as integer)
`This method uses bitwise operations to achieve a faster speed.
filter = num || (num >> 1)
filter = filter || (filter >> 2)
filter = filter || (filter >> 4)
filter = filter || (filter >> 8)
filter = (filter || (filter >> 16)) + 1
while filter
a$ = a$ + a$
If (num && filter) then a$ = a$ + char
filter = filter >> 1
endwhile
endfunction a$
function ReplaceString(char as string, num as integer)
a$ = replace all$(space$(num), " ", char)
endfunction a$