few days ago I felt the need to implement research about generic collections and began to find some method that allows me to search a generic collection type List (of T) to what I found the find method, but with very little help on him, and I turned & # 237; the ever useful newsgroup in which I received the help they need from one of the friends that there are always willing to help.
Well in short after seeing that was difficult but a bit scratched the method to implement the find command in the collection because it uses pointers to functions and global variables to the form, I use another class that is the SortedList (Of TKey, TValue) that vendrĂaa be ordered as a generic collection whose main feature is its easy and clean implementation. He also had the option of using the old for a sequential search for the collection, but this seemed somewhat outdated and inelegant , and was therefore to begin my research into search forms in a collection.
After testing with 1000000 of objects in a collection list (of T) and another SortedList (Of TKey, TValue) that the best way to work with many the old object is for or for each, if though I believe so, it turns out that although the SortedList (Of TKey, TValue) is much faster when do the search, is extremely inefficient in the time of loading, in my case, the uploaded million objects en 12 minutos!!! si, 12 minutos, mientras que la carga en una colección list(of T) tardó 3 segundos!!! increíble la diferencia no?. Eso si la búsqueda en el sortedlist no llegaba a un milisegundo mientras que en la list(of T) tardaba 80milisegundos con los for y entre 900 y 1100 milisegundos con el método find, la verdad que me sorprendió el resultado. Las pruebas las hice en una notebook pIV  de2.4Ghz con 512MB de memoria, me aseguré de buscar el último elemento de la colección.
Todos son muy fáciles de comprender, tal vez el mas "vuelteado" is using the find method because it uses global variables to form predicate and an object becomes a kind of pointer to the search function.
Well I spend my code that did the testing if you will see the implementation of these search methods
We call it a Winform frmColeccionesGenericas in which they placed 4 buttons each with a label below to show results (leave names by default), Each button is a type of load-search in a collection & # 243; n. Then go the codebehind and copied it, the following list is the full form
Public Class frmColeccionesGenericas
'global variable form to be used with the method , all find
Dim s_NombreABuscar As String
'global variable' Issue of items in the collection
Private nElementos As Integer = 1000000
'with the find method
Private Sub Button1_Click ( ByVal sender As System.Object, ByVal As and System.EventArgs) Handles Button1.Click Dim
ini As Date Dim
order As Date Dim
diff As TimeSpan
'---------------------carga la coleccion
ini = Now
For i As Integer = 1 To nElementos
        l.Add(
New cPersona(i, "John" & i, "321" & i)) Next
order = Now
fin.Subtract diff = (ini)
I . Label1.Text = "Time Charge" & diff.Minutes & ":" & diff.Seconds & " " & diff.Milliseconds
Load the collection
'--------------------- 'Create a Predicate object that points to the role fBuscador Dim
fBuscador As New Predicate (Of cPersona) (AddressOf Search)
'form variable
s_NombreABuscar = " , John " & nElementos
'----
search ini = Now
Dim p As cPersona = l.Find (fBuscador) end = Now
fin.Subtract diff = (ini)
I . Label1.Text & = vbCrLf & "Time Search" & diff.Minutes & ":" & diff.Seconds & ":" & diff.
Milliseconds End Sub
Private
Function Search (ByVal person As cPersona) As BooleanIf persona.Nombre.ToLower = s_NombreABuscar. ToLower Then
Return True
Else
Return False
End If
End Function
'with
SortedList Private Sub Button2_Click ( ByVal sender As System.Object, ByVal and As System.EventArgs) Handles Button2.Click
Dim ini As Date
Dim end The
Date Dim diff The TimeSpan
'--- load Justice ordered
Dim the The New SortedList (Of String , CPerson)
Dim p The CPerson
ini = Now
For and The Integer = 1 To nElementos
p =
New cPersona (i, "John" & i, "321" & i)
l.Add (p.Nombre, p)
Next
order = Now
fin.Subtract diff = (ini)
I . Label2.Text = "Duration:" & diff.Minutes & ":" & diff.Seconds & ":" & diff.Milliseconds
--- collection searches
ini = Now
p = l (
diff = fin.Subtract (ini)
I . Label2.Text & = vbCrLf & " ; Time Search: " & diff.Minutes & ":" & diff.Seconds & ":" & diff.Milliseconds
End Sub
'with a
for Private Sub Button3_Click ( ByVal sender As System.Object, ByVal and As System . EventArgs) Handles Button3.Click
Dim ini The Date
Dim end The
Date Dim diff The TimeSpan
' ---- Justice load
Dim the The New List (Of CPerson)
Dim p The CPerson
ini = Now For
i As Integer = 1 To nElementos
p =
New cPersona (i, "John" & i, "321" & i)
l.Add (p)
Next
order = Now
fin.Subtract diff = (ini)
I . Label3.Text = "Load Time" & diff.Minutes & ":" & diff.Seconds & ":" & diff.Milliseconds
'--- busca elements
being
= Now Dim s As String = "John" & nElementos
For to As Integer = 1 To nElementos
p = l(i)
If p.Nombre = s Then
      Exit For
End If
Next
fin = Now
diff = fin.Subtract(ini)
Me .Label3.Text &= vbCrLf & "Time Search" & diff.Minutes & ":" & diff.Seconds & ":" & diff. Milliseconds
End Sub
'with a' for each
Private Sub Button4_Click ( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim ini As Date
Dim fin As Date
Dim diff As TimeSpan
Dim l As New List( Of cPersona)
Dim p As cPersona
ini = Now For
i As Integer = 1 To nElementos
p =
New cPersona (i, "John" & i, "321" & i)
l.Add (p)
Next
order = Now
fin.Subtract diff = (ini)
I . Label4.Text = "Time de Carga " & diff.Minutes &" "& diff.Seconds &" "& diff.Milliseconds
ini = Now
For Each p In l
If p.Nombre = s Then
Exit For End
If
Next
order = Now
fin.Subtract diff = (ini)
End Sub
'Class to fill the collection
Class cPersona
Private nCode As Integer
Private sname As String
Private sTelefono As String
Public Property Cod() As Integer
Get
Return nCod
End Get
Set ( ByVal value As Integer )nCod = value
End Set
End Property
Public Property Nombre() As String
Get
Return sNombre
End Get
Set ( ByVal value As String )
sNombre = value
End Set
End Property
Public Property Telefono() As String
Get
Return sTelefono
End Get
Set ( ByVal value As String )
sTelefono = value
End Set
End Property
Public Sub New ( ByVal n_Cod As Integer , ByVal s_Nombre As String , ByVal s_Telefono As String )
nCod = n_Cod
sTelefono = s_Telefono Sname = s_Nombre
End Class
End Class
Well that's it for today
Salu2
Sergio
0 comments:
Post a Comment