Results 1 to 8 of 8

Thread: [RESOLVED] Question about the speed. See code.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Resolved [RESOLVED] Question about the speed. See code.

    Code:
    Private Sub cmdLoad_Click()
        Dim str As String
        Dim strA() As String
        Dim strB() As String
        Dim i As Long, j As Long
    
        '------------------------------WHY THIS ONE IS SPEEDY? FILE SIZE 20MB------------------------
    
    
        Open App.Path & "\raw.txt" For Input As #1
        str = StrConv(InputB(LOF(1), 1), vbUnicode)
        Close #1
    
        '------------------------------WHY THIS ONE IS SPEEDY? So many loops and functions-----------------------
    
        strA = Split(str, vbCrLf)
        j = 0
        ProgressBar.Max = UBound(strA) + 1
        ProgressBar.Min = 0
        ProgressBar.Value = 0
        For i = 0 To UBound(strA)
            DoEvents
            If InStr(1, strA(i), "DELIMITER") Then
                ReDim Preserve strB(j)
                strB(j) = strA(i)
                j = j + 1
            End If
            ProgressBar.Value = ProgressBar.Value + 1
        Next
    
    
        '------------Why this is SLOW, looks not much difference brom the previous section ----------------------
    
        ProgressBar.Max = UBound(strB) + 1
        ProgressBar.Min = 0
        ProgressBar.Value = 0
        For i = 0 To UBound(strB)
            DoEvents
            str = str & strB(i) & vbCrLf
            ProgressBar.Value = ProgressBar.Value + 1
        Next
    
        '---------------------- Haven't tested the following -----------
        Open App.Path & "\rawR.txt" For Output As #2
        Print #2, str
        Close #2
    
    End Sub
    Last edited by bPrice; Dec 9th, 2014 at 11:27 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Re: Question about the speed. See code.

    Code:
       '------------Why this is SLOW, looks not much difference brom the previous section ----------------------
    
        ProgressBar.Max = UBound(strB) + 1
        ProgressBar.Min = 0
        ProgressBar.Value = 0
        For i = 0 To UBound(strB)
            DoEvents
            str = str & strB(i) & vbCrLf
            ProgressBar.Value = ProgressBar.Value + 1
        Next
    I tried this and it runs at a heaven's speed ...

    Code:
        str = Join(strB, vbCrLf)

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Question about the speed. See code.

    Those pieces of code in the OP are all doing different things, You say two are speedy and one is slow but what does that even mean and what are they doing really?

    DoEvents can slow things down as well as introduce potential problems. Refresh is a better choice in such a case.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Re: Question about the speed. See code.

    Quote Originally Posted by DataMiser View Post
    Those pieces of code in the OP are all doing different things, You say two are speedy and one is slow but what does that even mean and what are they doing really?

    DoEvents can slow things down as well as introduce potential problems. Refresh is a better choice in such a case.
    1, I have a text file. Now I use Open and InputB function, to load it into a string variable. This is fast even though the file is 20mb.

    2, After this, I split this string into different parts (20000+) and according to a certain standard, I then load the string parts into a string array, and this is fast, too.

    3, Last I use For Loop, to save each element in this string array to one string variable. This is where things get very slow. I mean, 100 times slower. However, during the whole process, the data in total didn't increase. It's just the ways I handled the data are different.

    Code:
      '------------Why this is SLOW, looks not much difference brom the previous section ----------------------
    
        ProgressBar.Max = UBound(strB) + 1
        ProgressBar.Min = 0
        ProgressBar.Value = 0
        For i = 0 To UBound(strB)
            DoEvents
            str = str & strB(i) & vbCrLf
            ProgressBar.Value = ProgressBar.Value + 1
        Next
    Later I tried to use JOIN() and it's also fast, 100 times more faster. I'm looking for explanations.

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,902

    Re: Question about the speed. See code.

    This makes the code slow:
    Code:
    str = str & strB(i) & vbCrLf
    Every time the string needs to be reallocated to add 2 parts of data.
    More efficient would be the calculate the total size of the target string, allocate this size to the string and then use the mid() method to update the string.
    Or you could use some external stringbuilder class for this.

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Question about the speed. See code.

    Are you really using a raw.txt file with the word "DELIMITER" written in it , say, thousands of times on each line?

    If not (I doubt so), the if statement :

    If InStr(1, strA(i), "DELIMITER") Then

    will never evaluate true, no Redim Preserve on strB(j) will ever take place. That in itself would explain the speed of the first loop.

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Question about the speed. See code.

    This explains what Arnoutdv was saying in further detail. https://support.microsoft.com/kb/170964?wa=wsignin1.0
    Bottom line is concatination is very slow. The mid method or use of an array are the ways to go.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Question about the speed. See code.

    Yeah, it's the string concatenation that's the chokepoint. Strings are immutable, so when you append strings it has to re-allocate new memory and copy everything over to the new space. Using the replace method as suggested by Arnoutdv is a better way to go because it doesn't change the size, making allocation a lot faster. I think new strings still get created, but it's still better that straight up concatenation. My instinct was to use the join method ... so I'd compare the join operation with Arnoutdv's suggestion and see how they compare.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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