Results 1 to 9 of 9

Thread: [RESOLVED] How can I span faster?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Alabama
    Posts
    15

    Resolved [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:
    1. Public Sub Span(bIn() As Byte, bOut() As Byte, Optional sBreak As String = vbCrLf)
    2. Dim i As Long
    3. Dim sLong As String
    4. Dim sSpan As String
    5. Dim sLineBreak As String
    6. Dim lLineLength As Long
    7.  
    8.     ' change these parts to alter the character
    9.     ' used for the line break
    10.     sLineBreak = sBreak
    11.     lLineLength = 76 - Len(sLineBreak)
    12.  
    13.     ' firstly convert the array to a string
    14.     ' this makes the whole operation much easier
    15.     sLong = StrConv(bIn, vbUnicode)
    16.    
    17.     ' now add the line breaks
    18.     i = 1
    19.    
    20.     For i = 1 To Len(sLong) Step lLineLength
    21.         ' collect 75 chars and add a vbCrLf
    22.         sSpan = sSpan & Mid$(sLong, i, lLineLength) & vbCrLf
    23.     Next i
    24.    
    25.     ' convert back into a byte array
    26.     bOut = StrConv(sSpan, vbFromUnicode)
    27.    
    28. 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?

  2. #2

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Alabama
    Posts
    15

    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:
    1. For i = 1 To Len(sLong) Step lLineLength
    2.         ' collect 75 chars and add a vbCrLf
    3.         sSpan = sSpan & Mid$(sLong, i, lLineLength) & vbCrLf
    4.     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.

  3. #3
    New Member
    Join Date
    Mar 2006
    Posts
    1

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Alabama
    Posts
    15

    Re: How can I span faster?

    It has to be in chunks of 76 characters though.

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Alabama
    Posts
    15

    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:
    1. NewLength = ((Len(sLong) \ 76) * 78) + (Len(sLong) Mod 76)
    2.     Dim sSpan As String * NewLength
    3.  
    4.     For i = 1 To NewLength Step lLineLength
    5.         ' collect 75 chars and add a vbCrLf
    6.         Mid$(sSpan, i, 76) = Mid$(sLong, i, 74)
    7.         sSpan = sSpan + vbCrLf
    8.     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?

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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:
    1. ' start a new project and add a multiline textbox
    2. Private Sub Form_Load()
    3.     Dim sLong As String, sSpan As String, NewLength As Long, i As Long, line As Long
    4.    
    5.     ' just a sample string so we can see that it works
    6.     sLong = String$(512, "a")
    7.     ' count the new length of the string: reserve two bytes after each line for vbCrLf
    8.     ' also add two more as the last character will be vbCrLf
    9.     NewLength = ((Len(sLong) \ 76) * 78) + (Len(sLong) Mod 76) + 2
    10.     ' fill end result string
    11.     sSpan = String$(NewLength, "b") & "And it works"
    12.  
    13.     ' loop through all lines
    14.     For i = 1 To NewLength Step 76
    15.         ' store line to the new position and add the vbCrLf
    16.         Mid$(sSpan, i + line, 78) = CStr(Mid$(sLong, i, 76) & vbCrLf)
    17.         ' vbCrLf extra character counter
    18.         line = line + 2
    19.     Next i
    20.    
    21.     ' sample output for debugging purposes
    22.     Text1.Text = sSpan
    23. End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Alabama
    Posts
    15

    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Alabama
    Posts
    15

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width