-
Feb 5th, 2011, 11:57 PM
#1
Thread Starter
Addicted Member
[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.
-
Feb 6th, 2011, 12:20 AM
#2
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.
-
Feb 6th, 2011, 02:15 PM
#3
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.
-
Feb 6th, 2011, 02:57 PM
#4
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
-
Feb 6th, 2011, 06:56 PM
#5
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
-
Feb 6th, 2011, 07:20 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|