Article View: free.it.visual-basic
Article #3135oggetto Collection
From: luckydor
Date: Mon, 21 Oct 2002 15:05
Date: Mon, 21 Oct 2002 15:05
117 lines
4123 bytes
4123 bytes
Qual'� il sistema pi� veloce x trovare una stringa
abinata ad un'altra e ad un indice.
Ad esempio:
Alla stringa "Dieci" e abinata "Ten" con indice 10.
Un oggetto Collection mi sembrava la soluzione migliore.
Ma mi sono accorto che loopando una matrice � decisamente + veloce.
Eppure dall'help:
"Inoltre, a differenza delle matrici, gli oggetti Collection consentono una
ricerca indicizzata estremamente veloce."
X non farVi perdere tempo ecco il codice:
''''''''''' nel Form inserire un CommandButton index 0'''
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim Mita(10), Musa(10), MYcol As New Collection
Sub indice_in_matrice()
Dim cerca As Integer, trovato As String
cerca = 10
start = GetTickCount
For volte = 1 To 100000 ' 0,08~
trovato = Mita(cerca)
Next
Caption = trovato & " Tempo = " & (GetTickCount - start) / 1000
End Sub
Sub string_in_matrice()
Dim cerca As String, trovato As String
cerca = "Dieci"
start = GetTickCount
For volte = 1 To 100000 ' 0,800~
For idx = 0 To 10
If cerca = Mita(idx) Then Exit For
Next
Next
Caption = Musa(idx) & " Tempo = " & (GetTickCount - start) / 1000
End Sub
Sub indice_in_Collection()
Dim cerca As Integer, trovato As String
cerca = 11
' ' ' ' '
start = GetTickCount
For volte = 1 To 100000 ' 0,18~
trovato = MYcol.Item(cerca)
Next
Caption = trovato & " Tempo = " & (GetTickCount - start) / 1000
End Sub
Sub key_in_Collection()
Dim cerca As String, trovato As String
cerca = "Ten"
' ' ' ' '
start = GetTickCount
For volte = 1 To 100000 ' 2,140~
trovato = MYcol.Item(cerca)
Next
Caption = trovato & " Tempo = " & (GetTickCount - start) / 1000
End Sub
Private Sub Command1_Click(Index As Integer)
CallByName Me, Command1(Index).Caption, VbMethod
End Sub
Private Sub Form_Load()
sss =
"indice_in_matrice,string_in_matrice,indice_in_Collection,key_in_Collection"
sss = Split(sss, ",")
Command1(0).Caption = sss(0)
For idx = 1 To 3
Load Command1(idx): Command1(idx).Visible = 1: Command1(idx).Caption
= sss(idx)
Next
matrix = "Zero,Uno,Due,Tre,Quattro,Cinque,Sei
,Sette,Otto,Nove,Dieci,Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten
"
matrix = Split(matrix, ",")
For idx = 0 To 10
Mita(idx) = matrix(idx): Musa(idx) = matrix(idx + 11)
MYcol.Add matrix(idx), matrix(idx + 11)
Next
End Sub
Private Sub Form_Resize()
Me.Scale (0, 0)-(1, 4)
For idx = 0 To 3
Command1(idx).Move 0, idx, 1, 1
Next
End Sub
'''''''''''''''''''''''''END''''''''
Eppure le matrici le ho date Variant.
Oltretutto con le matrici posso cercare le informazioni
in tutti i modi,
mentre con Collection con l'indice o con Key.
Mi spiegate che convenienza c'� ad utilizzare le Collection.
Qualsiasi consiglio x ottenere lo stesso risultato
+ velocemente
mi sarebbe davvero gradito.
Grazie in ogni caso.
Technical Details
Message-ID:
<DIUs9.18420$TD1.816846@news2.tin.it>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!newsmi-us.news.garr.it!NewsITBone-GARR!news.mailgate.org!nntp.infostrada.it!news-out.tin.it!news-in.tin.it!news2.tin.it.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ITALIAN,ASCII X-Google-Thread: f94ee,b81cc769d6a3be1f,start X-Google-Attributes: gidf94ee,public X-Google-ArrivalTime: 2002-10-21 08:12:06 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!newsmi-us.news.garr.it!NewsITBone-GARR!news.mailgate.org!nntp.infostrada.it!news-out.tin.it!news-in.tin.it!news2.tin.it.POSTED!not-for-mail From: "luckydor" <orfwim@tin.it> Newsgroups: free.it.visual-basic Subject: oggetto Collection Lines: 120 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Message-ID: <DIUs9.18420$TD1.816846@news2.tin.it> Date: Mon, 21 Oct 2002 15:05:07 GMT NNTP-Posting-Host: 213.45.114.186 X-Complaints-To: newsmaster@tin.it X-Trace: news2.tin.it 1035212707 213.45.114.186 (Mon, 21 Oct 2002 17:05:07 MET DST) NNTP-Posting-Date: Mon, 21 Oct 2002 17:05:07 MET DST Organization: TIN