Re: Generic List Find.

Tech-Archive recommends: Fix windows errors by optimizing your registry



Well, I stand corrected.... Find is faster then using Enumeration, even when
you have to go through the contortions that Tom suggested. The results for
the test code that follows were:

Starting test.....
Elasped time for Enumeration is 00:00:13.0926257
Elasped time for Find is 00:00:12.4814362

and the test I used....

Module Module1
Dim Products As New List(Of Product)
Dim TestCount As Integer = 10000

Sub Main()
Dim I As Integer
For I = 1 To TestCount
Dim p As New Product(I)
Products.Add(p)
Next
Dim sw As New System.Diagnostics.Stopwatch
Console.WriteLine("Starting test.....")
sw.Start()
For I = 1 To TestCount
FindProduct(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Enumeration is {0}", sw.Elapsed)
sw.Reset()
sw.Start()
For I = 1 To TestCount
FindProduct2(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Find is {0}", sw.Elapsed)
Console.ReadLine()
End Sub

Private Function FindProduct(ByVal code As String) As Product
For Each p As Product In Products
If p.Code = code Then Return p
Next
Return Nothing
End Function

Function FindProduct2(ByVal code As String) As Product
Dim pred As New PredicateClass(code)

Return products.Find(AddressOf pred.PredicateFunction)
End Function

Public Class Product
Private _Code As String
Public ReadOnly Property Code() As String
Get
Return _Code
End Get
End Property

Public Sub New(ByVal c As Integer)
_Code = c.ToString
End Sub
End Class

Private Class PredicateClass
Private code As String
Public Sub New(ByVal code As String)
Me.code = code
End Sub

Public Function PredicateFunction(ByVal bo As Product) As Boolean
Return bo.Code = Me.code
End Function
End Class

End Module
--
Terry


"Terry" wrote:

I had assumed in this example that GetProducts() was just going to return a
reference to an existing List. The real point I was making here, was that,
while both Satish and Tom were lamenting the lack of 'anonymous methods ' in
VB, why would you use one in this case. And I think that holds true for both
C# and VB. That is unless 'Find' has some built in speed improvemnt that
will make up for a totally un-needed function call.
--
Terry


"Lloyd Sheen" wrote:


"Terry" <TerryL@xxxxxxxxxxxxx> wrote in message
news:004C4387-3027-4775-BD38-372D843D8C98@xxxxxxxxxxxxxxxx
Why not just code it the straight forward way?

Function FindProduct(code as string) as Product
dim products as List(of Product) = GetProduct()
for each p as product in Products
if p.code = code then
return p
end if
return nothing
end function


--
Terry


"Satish Itty" wrote:

How do I write the following c# code in vb

Product FindProduct(string code)
{
List<Product> products = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });
}


Can you change or overload the GetProduct to take a code as a parameter.
Seems like alot of extra processing to retrieve all the info and then filter
it.

LS


.



Relevant Pages

  • Re: Incompatibility between Access 2003 and Access 2002
    ... Private WithEvents mlst As ListBox ... Private mot As ObjectType ... Public DisplayField As String ... Dim prm As DAO.Parameter ...
    (microsoft.public.access.modulesdaovba)
  • Re: FileSystemWatcher advice required please
    ... Private ArchiveImport As String ... Private FilesToProcess As ProcessFiles ... Public Sub Main ... Dim NoVersion As New Collection ...
    (microsoft.public.dotnet.framework)
  • Re: Is there a way to prevent a RichTextBox from scrolling?
    ... Private _isRegex As Boolean ... Public Sub New(ByVal thispattern As String, ... Dim entry As tDict ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • MAPI Emails from Access
    ... I realize this code is quite long, but could someone take a look at the sub ... Private Const mcERR_DOH = vbObjectError + 10000 ... Private mstStatus As String ... Dim db As Database, rs As Recordset ...
    (microsoft.public.access.formscoding)
  • Re: File attributes
    ... Private Declare Function GetFullPathName Lib "kernel32" Alias ... ByVal lpFileName As String, _ ... Dim Buffer As String ... Private Const VOS_UNKNOWN = &H0 ...
    (microsoft.public.excel.programming)