Lets say we have a generic list of Class1, typically having ~100 objects for a given session.
I would like to see if the list has a particular object. ASP.NET 2.0 allows me to do this:
Dim objResult as Class1 = objList.Find(objSearch)
How does this approach rate when compared to a traditional For loop, looking at a performance perspective?
How would this vary with increase or decrease in length of the list?
You can easily see how .Net
Listimplements theFindmethod using Reflector:The only difference between both implementations is that
Findrequires a call tomatchinstead of having this logic inlined in the loop.Interestingly, this simple performance:
Shows that:
Total time required to query the list using Find is 05.7990078 seconds.
Total time required to query the list using loop is 06.3551074 seconds.
This results seems consistent in several executions.
Edit – Found explanation for the advantage of
Find:Findworks faster since it accesses directly the underlying array in each iteration. The loop access it through theListindexer, which requires for every access index verification: