Code: (Select All)
Function FillString2 (Size As Long, Bases As String, S As String)
'Tempo's original routine modified by STEVE in an attempt to improve speeds.
'It worked a widdle bit.
$Checking:Off
FillString2 = 0
Dim posi As Long
S = Space$(Size)
posi = 1
b$ = Bases
Do
count = count + 1
l = Len(b$)
Mid$(S, posi, l) = b$
posi = posi + l
If count Mod 10 = 0 Then b$ = Left$(S, posi - 1)
Loop Until posi > Size
S = Left$(S, Size)
FillString2 = -1
$Checking:On
End Function
For anyone who hasn't looked closely at this topic, I'd suggest you guys take a moment to study the out of the box thinking that produced the masterpiece of speed above. Note that this little routine is even faster than _MEMFILL by about 200-300%!!
The trick here?
Mixing inefficiencies to try and minimize them as much as possible.
Adding strings to stings repeatedly is inefficient. Running loops a bazillion times is inefficient. So how do we try to speed this up and reduce the impact as much as possible on both?
We start out with a fill string of b$, which in this case is "1234".
After 10 passes, we redefine b$ to be the current string. "1234123412341234123412341234123412341234" We're now filling at 10 times the original rate. What would've taken us 100 passes to fill, we now do in 10.
And then, after 10 more passes, we redefine b$ again. It's now "1234" repeated 100 times. We're now filling at 100 times the original rate.
And then, after 10 more passes, we refine b$ again. It's now "1234" repeated 1000 times.
The amount of string addition that we're doing here is minimal, and the overall number of passes that we have to make to do the filling here has decreased exponentially.
You can play with the amount of loops where b$ redefines itself with changing this line:
If count Mod 10 = 0 Then b$ = Left$(S, posi - 1)
As is, it's running every 10th loop and redefining b$ to become the current string. Change that 10 to some other number and play around with how it affects performance. I just threw a few quick blips at it and tested values like 2, 10, 20... 10 seemed a sweet spot and faster than 2 or 20, but there may be another value that's even faster. The trick is to find the perfect point where the hit with sting addition doesn't exceed the number of repetitions needed for the string to fill.
It's an attempt to minimize the impact of both bottlenecks to performance, and I've got to say, I'm pretty dang happy with the results. Folks want to know how to maximize and improve speed all the time. It's little tricks like this, where you look to minimize known impacts (such as the slowness of repetitive string addition) and looping, to make things work as quickly as possible for you.
