Results 1 to 7 of 7

Thread: Optimizing Loop

  1. #1

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Optimizing Loop

    How can I optimize this loop?:

    VB Code:
    1. For I = 0 To UBound(hk) - 1
    2.  
    3. For D = 0 To UBound(sono) - 1
    4. If InStr(1, Trim(sono(D)), Trim(hk(I))) > 0 Then
    5. sAll = sAll & vbCrLf & Trim(hk(I)) & "  ***PASSER***"
    6. GoTo NextLoop
    7. End If
    8.  
    9. Next
    10. sAll = sAll & vbCrLf & Trim(hk(I)) & "  ***PASSER IKKE***"
    11. NextLoop:
    12. Next

    What I need to do is see if any of the values in 'hk' are similar to any of the values in 'sono'.

    A friend told me of something called binary sorting - if this is a possibility, how would I do it?

    Cheers!
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    the sorting idea could possibly be useful, how many items are in each array?

    here's an improved version of what you posted:
    VB Code:
    1. Dim max_hk as long
    2. Dim max_sono as long
    3. Dim MatchFound as Boolean
    4. Dim CurrentHK as String
    5.  
    6. max_hk = UBound(hk) - 1
    7. max_sono = UBound(sono) - 1
    8.  
    9. For I = 0 To max_hk
    10.  
    11.   MatchFound = False
    12.   CurrentHK = Trim(hk(I))
    13.  
    14.   For D = 0 To max_sono
    15.     If InStr(1, Trim(sono(D)), CurrentHK) > 0 Then
    16.       sAll = sAll & vbCrLf & CurrentHK & "  ***PASSER***"
    17.       MatchFound = True
    18.       Exit For
    19.     End If
    20.   Next D
    21.  
    22.   If Not MatchFound Then
    23.     sAll = sAll & vbCrLf & CurrentHK & "  ***PASSER IKKE***"
    24.   End If
    25.  
    26. Next I

  3. #3

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    well

    Thanks for your optimization!

    Gonna try it out, see if it's enough to be acceptable =).

    But - The number of items in the arrays are declared to 4000 for hk, and 8000 for sono - Though this isn't toally correct figures(there are some blank vars), it's close to it, with a margin of 50-70 max.

    Cheers!
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    cool.. let us know the time it takes (for both the old & new versions). Make sure that I and D are both declared as Long.


    As you have about 32 million checks it will take a fair amount of time, especially with InStr.

    unfortunately binary sorting would be completely useless with InStr... are you sure you need it? (are you just intending to compare the first few characters?)

    if a sono() matches one hk(), is it possible that it will match another hk()?

  5. #5

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Well

    InStr isn't completely neccessary, i'll just use Trim() instead.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    in that case a binary search would be possible... I'll post a description/example tomorrow if I remember!

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I had a little thought on how to speed it up... convert "sono" to a single string, then just do an InStr on that string (once) for each "hk".

    This is about 3 times faster (for random data) than my previous post, and about 5 times faster than your original version:
    VB Code:
    1. Dim i As Long, d As Long
    2. Dim max_hk As Long
    3. Dim CurrentHK As String
    4. Dim sono_str As String
    5.  
    6. max_hk = UBound(hk) - 1
    7.  
    8. 'fill the string with the entire array, with each item surrounded by character 0
    9. sono_str = Chr(0) _
    10.          & Join(sono, Chr(0)) _
    11.          & Chr(0)
    12.  
    13. For i = 0 To max_hk
    14.  
    15.   CurrentHK = Trim(hk(i))
    16.  
    17.   If InStr(1, sono_str, Chr(0) & CurrentHK [i]& Chr(0)[/i]) Then
    18.     sAll = sAll & vbCrLf & CurrentHK & "  ***PASSER***"
    19.   Else
    20.     sAll = sAll & vbCrLf & CurrentHK & "  ***PASSER IKKE***"
    21.   End If
    22.  
    23. Next i
    If HK is likely to be the shorter of the two values then the InStr could have the final Chr(0) removed to add more functionality. As it is the two must be the same, with that change they just need to start the same.

    To be honest, unless your data is too large for this approach then I dont see a binary search being worthwhile.

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