Searching List<T>, how to make it faster

  • Thread starter Thread starter M.H.
  • Start date Start date
M

M.H.

I have List<T> with about 6000-7000 objects.
I need to select all objects that the property Name starts with
SearchText.

This is the code

foreach (T item in myList)
if (item.Name.StartsWith(SearchText))
myResults.Add(item);

It takes about 5-6 seconds and my users are not happy with the speed.

How to improve it?
I tried "for (int.." but the result was the same.

Thanks

MH
 
Is this DB populated?

I suggest you need to revist your design. Your best bet would be to populate
your list based on a DB query with a parameter called name.

Select * from Table where Name like 'x%'

alternatively you could try linq or the entity framework.
 
Is this DB populated?

I suggest you need to revist your design. Your best bet would be to populate
your list based on a DB query with a parameter called name.

Select * from Table where Name like 'x%'

alternatively you could try linq or the entity framework.
 
Thousands of objects are unlikely genereated on the fly, so maybe the
overhead of having the data stored in a database could be minimal, and thus,
with an index on the field name, that query should be almost instantaneous
(human perception). Note that ending-with won't get much improvment from
an index, but starting-with does.


So, using a database (and LINQ) ?


Vanderghast, Access MVP
 
Thousands of objects are unlikely genereated on the fly, so maybe the
overhead of having the data stored in a database could be minimal, and thus,
with an index on the field name, that query should be almost instantaneous
(human perception). Note that ending-with won't get much improvment from
an index, but starting-with does.


So, using a database (and LINQ) ?


Vanderghast, Access MVP
 
Thanks Paul.
Is this DB populated?
My objects are created by reading data from two servers that aren't
linked. They are sorted by date.
I suggest you need to revist your design.
I think I will use ABC treeview (with no parent, no room on the screen)
for searching.

There are 3 users using this screen and I calculated they are loosing
maybe 5 minutes in total per day waiting for the search results. But
they are just not happy.

MH
 
Thanks Paul.
Is this DB populated?
My objects are created by reading data from two servers that aren't
linked. They are sorted by date.
I suggest you need to revist your design.
I think I will use ABC treeview (with no parent, no room on the screen)
for searching.

There are 3 users using this screen and I calculated they are loosing
maybe 5 minutes in total per day waiting for the search results. But
they are just not happy.

MH
 
Also, check your Name property if it's doing any extra processing when
the get accessor is called.
 
Also, check your Name property if it's doing any extra processing when
the get accessor is called.
 
Also, check your Name property if it's doing any extra processing when
the get accessor is called.

It was that. Plenty of stupid code behind Name property.
Now all is fine.

MH
 
Also, check your Name property if it's doing any extra processing when
the get accessor is called.

It was that. Plenty of stupid code behind Name property.
Now all is fine.

MH
 
Keep them in sorted order and do a binary search.  There is a SortedList
type that should help.  (Weren't we just asking the other day whether
binary search should be required knowledge for programmers?)

Why not write the whole application in assembly? Over-optimization.

Get a beginners level book on .NET Performance tuning and optimization.
 
Keep them in sorted order and do a binary search.  There is a SortedList
type that should help.  (Weren't we just asking the other day whether
binary search should be required knowledge for programmers?)

Why not write the whole application in assembly? Over-optimization.

Get a beginners level book on .NET Performance tuning and optimization.
 
Back
Top