[RESOLVED] How can I span faster?
What I have is code that takes data that is converted to base64 and spans it to 75 characters and a vbCrLf per line. Here is the code including the variables for that:
VB Code:
Public Sub Span(bIn() As Byte, bOut() As Byte, Optional sBreak As String = vbCrLf)
Dim i As Long
Dim sLong As String
Dim sSpan As String
Dim sLineBreak As String
Dim lLineLength As Long
' change these parts to alter the character
' used for the line break
sLineBreak = sBreak
lLineLength = 76 - Len(sLineBreak)
' firstly convert the array to a string
' this makes the whole operation much easier
sLong = StrConv(bIn, vbUnicode)
' now add the line breaks
i = 1
For i = 1 To Len(sLong) Step lLineLength
' collect 75 chars and add a vbCrLf
sSpan = sSpan & Mid$(sLong, i, lLineLength) & vbCrLf
Next i
' convert back into a byte array
bOut = StrConv(sSpan, vbFromUnicode)
End Sub
The problem is that it takes more than four or five minutes to span data that is around 600kb. This code is all in a module and the language is VB6. How can I do this faster?
Re: How can I span faster?
Hrm.
Was my question not clear, or is everyone just as stumped as me? The main problem lies within these lines:
VB Code:
For i = 1 To Len(sLong) Step lLineLength
' collect 75 chars and add a vbCrLf
sSpan = sSpan & Mid$(sLong, i, lLineLength) & vbCrLf
Next i
I have added a progress bar to show the progress of the spanning, and it seems that the code starts up at a good speed, then shows down considerably by the time it nears completion. Any explanations for this?
Thanks.
Re: How can I span faster?
Hi,
that's easy to solve.
The longer the text (sSpan) the longer the & to sSpan.
Make chunks and you will see its much faster.
e.g.
By Jeanie
Re: How can I span faster?
It has to be in chunks of 76 characters though.
Re: How can I span faster?
It keeps getting slower because each time you handle strings, you copy memory from place to another. So when the string gets longer it just keeps getting slower as more memory is required to be moved around.
Easiest method to go around this problem is to first reserve the required amount of memory using String$ and then doing Mid$(dest) = Mid$(source) and you'll be copying the data much faster. You just have to precalculate the final size of the string beforehand, which is as simple as
NewLength = ((Len(source) \ 76) * 78) + (Len(source) Mod 76)
Yet another method is to copy the data to byte array and handle the copying that way.
The most ultimate way you can use in VB6 is to change SAFEARRAYHEADER of an integer array to point to the string and copy data from an integer array to another. Search for SisicM to find a InStrCount function by me that uses this method if interested.
Pretty much the same technique is used in all of these, precalculate the final size and copy data directly to there.
Re: How can I span faster?
Ok, first off, thanks for the reply.
Secondly, I tried the first method you suggested, but it doesn't seem to be actually moving the data from one string to the next. Here is what I came up with:
VB Code:
NewLength = ((Len(sLong) \ 76) * 78) + (Len(sLong) Mod 76)
Dim sSpan As String * NewLength
For i = 1 To NewLength Step lLineLength
' collect 75 chars and add a vbCrLf
Mid$(sSpan, i, 76) = Mid$(sLong, i, 74)
sSpan = sSpan + vbCrLf
Next i
sSpan is supposed to be the final string, and sLong is where it needs to be copied from, but it isn't actually copying it. Any suggestions?
Re: How can I span faster?
You're filling out the line changes, that is why it doesn't work.
I'm too tired to start explaining what to change (had four hours of sleep during night), so here is the code that'll speak for itself :)
VB Code:
' start a new project and add a multiline textbox
Private Sub Form_Load()
Dim sLong As String, sSpan As String, NewLength As Long, i As Long, line As Long
' just a sample string so we can see that it works
sLong = String$(512, "a")
' count the new length of the string: reserve two bytes after each line for vbCrLf
' also add two more as the last character will be vbCrLf
NewLength = ((Len(sLong) \ 76) * 78) + (Len(sLong) Mod 76) + 2
' fill end result string
sSpan = String$(NewLength, "b") & "And it works"
' loop through all lines
For i = 1 To NewLength Step 76
' store line to the new position and add the vbCrLf
Mid$(sSpan, i + line, 78) = CStr(Mid$(sLong, i, 76) & vbCrLf)
' vbCrLf extra character counter
line = line + 2
Next i
' sample output for debugging purposes
Text1.Text = sSpan
End Sub
Re: How can I span faster?
Ok, thank you very much for all your help. I was beginning to lose hope. You have been very helpful. Thanks.
Re: [RESOLVED] How can I span faster?
Ok, I have implemented that code into my code, and now files that previously took several minutes to convert now are converted nearly instantly. Thanks again.