Results 1 to 6 of 6

Thread: [RESOLVED] Please help me for search in big array.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Resolved [RESOLVED] Please help me for search in big array.

    Dear all expert programmers,
    I plan to create some function to count number of members's name that is content specified character and specified position of character and want fast process. Please optimize my code to make it fast if max array is 32767"

    Code:
    Option Explicit
    Private Members(3) As String
    Private Sub Command1_Click()
        Members(0) = "Albert"
        Members(1) = "Ronal"
        Members(2) = "Micheal"
        Members(3) = "Alex"
        
        MsgBox CountMembersContentChar("A", 1) & vbCrLf & CountMembersContentChar("o", 2)
        
    End Sub
    
    Private Function CountMembersContentChar(ByVal strChar As String, ByVal lngPos As Byte) As Long
        Dim idx As Long
        Dim iCnt As Long
        For idx = 0 To UBound(Members)
            If lngPos <= Len(Members(idx)) Then
                If InStr(1, Members(idx), strChar, vbBinaryCompare) = lngPos Then
                    iCnt = iCnt + 1
                End If
            End If
        Next
        CountMembersContentChar = iCnt
    End Function
    Thank you for all post.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Please help me for search in big array.

    Well, sorting might help only if looking for the 1st character in the name. But your requirements say the character can be in any specific position. So, sorting won't help in the other cases.

    Normally, I'd say building an indexing array would help dramatically. But the problem there is that if you have an absolutely large array, then creating & filling additional indexing arrays may take more time in the beginning and rebuilding them as items are added/removed/swapped can take even more time.

    You might want to conisider a database or disconnected recordset which doesn't require a database. Then you'd be able to use LIKE statments and allow the optimized code of a recordset to do the work for you. Just an idea. I'm sure others will chime in with other options.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Please help me for search in big array.

    I agree with the database they are built for such tasks, mysql is free, setting up is not too bad, you can download xampp which comes with apache and mysql.

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Please help me for search in big array.

    Forgetting about databases for a minute and looking at your existing code... It would be better to use Mid$() over Instr(). Better still, double lngPos when the function enters and then use LenB() for the length and MidB$() for the character.
    Code:
    Private Function CountMembersContentChar(ByVal strChar As String, ByVal lngPos As Byte) As Long
        Dim idx As Long
        Dim iCnt As Long
        lngPos = lngPos * 2
        For idx = 0 To UBound(Members)
            If lngPos <= Lenb(Members(idx)) Then
                If MidB$(Members(idx), lngpos, 2) = strChar Then
                    iCnt = iCnt + 1
                End If
            End If
        Next
        CountMembersContentChar = iCnt
    End Function
    (untested)
    Last edited by Milk; Feb 6th, 2011 at 03:02 PM. Reason: added some code
    W o t . S i g

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

    Re: Please help me for search in big array.

    Still better:
    Code:
    ' ByRef instead of ByVal: no new string created
    ' lngPos: Long instead of Byte, Long is faster
    Private Function CountMembersContentChar(ByRef strChar As String, ByVal lngPos As Long) As Long
        Dim idx As Long
        Dim iCnt As Long
        Dim iChr As Integer
        ' cache the character code value
        iChr = AscW(strChar)
        ' Milk forgot to account for Base 1 with Mid$ -> MidB$ change
        lngPos = (lngPos - 1) * 2 + 1
        For idx = 0 To UBound(Members)
            If lngPos <= LenB(Members(idx)) Then
                ' numeric comparison is way faster (compiled!)
                If AscW(MidB$(Members(idx), lngPos, 2)) = iChr Then
                    iCnt = iCnt + 1
                End If
            End If
        Next
        CountMembersContentChar = iCnt
    End Function

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Please help me for search in big array.

    It's been over a year since I used VB6 and I am now officially rusty.
    W o t . S i g

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