Feb 5 2010

Find it faster amongst the collection

Category: Tips and TricksJoeGeeky @ 04:00

Here's a quick tip; pun intended. We have all had to find something in a Collection or List and in many cases it probably looked something like the following:

var tokens = new List<string>();
...
if (tokens.Contains("Foo")) { /* Do something */ }
 

Pretty simple, but if you are looking for multiple values you might have something like this:

if (tokens.Contains("Foo")) { /* Do something */ }
else if (tokens.Contains("Bar")) { /* Do something else */ }

or even this...

foreach (var searchToken in searchTokens)
{
    if (tokens.Contains(searchToken)) { /* Do something */ }
}

All these work fine, but under a large load or with very large collections you may find that these patterns are incredibly slow. There is however another approach that is substantially faster and most people I've spoke to never even knew it was an option. The alternative...

IEnumerable<string> values = tokens.Intersect(new[] { "Foo", "Bar" });

This is a nice simple call, even if you are only looking to match a single value. Elegance aside, the important bit is the math...  Is it really faster? Your results may vary somewhat but here are some numbers; in ticks; showing the difference between Contains and Intersect in a large collection. The size of the collection is not really important, as the point lies in the relative difference between each approach... Alright... it was a collection with 21.5 million entries, and each approach searched for the same five values.

  • Using .Contains five times: 1,015,560 ticks
  • Using one .Intersect call with five search values: 71,739 ticks

Keep in mind this was done a simple desktop to your results may vary... As it turns out a .BinarySearch(x) run five times returned the same values in 230 ticks, but this approach has limitations that you will have to read about on your own. Do your own testing, and you may find this a useful alternative and might make the difference between success and failure in a production scenario.

Tags:

Comments

1.
Mark Mark Canada says:

.ToLookup( ... ) can be equally valuable.

2.
Bob Cravens Bob Cravens United States says:

Nice...didn't know that was there. Thanks!

3.
pingback blog.cwa.me.uk says:

Pingback from blog.cwa.me.uk

The Morning Brew - Chris Alcock  » The Morning Brew #534

4.
Andrew Baxter Andrew Baxter United States says:

Dude.. I am not much into reading, but somehow I got to read lots of articles on your blog. Its amazing how interesting it is for me to visit you very often.

5.
Tagesgeld Tagesgeld Germany says:

You own a very interesting blog covering lots of topics I am interested as well.I just added your site to my favorites to continue reading in the future... Please continue your excellent work

6.
Ricky Ricky Nicaragua says:

This is a real attention grabber about this subject. Nice to see someone get it right.
Last week I read a really boring report..this was refreshing.

7.
Javier Whitlow Javier Whitlow United States says:

Hi thank you for an perceptive post, I actually found your blog by mistake while searching on Goole for something else nearly related, in any case before i ramble on too much i would just like to state how much I enjoyed your post, I have bookmarked your site and also taken your RSS feed, Once Again thank you very much for the blog post keep up the good work.

8.
Del Imes Del Imes United States says:

Although I would’ve preferred if you went into a little bit more detail, I still got the gist of what you meant. I agree with it. It might not be a popular idea, but it makes sense. Will definitely come back for more of this. Great work

9.
Sasha Rathgeber Sasha Rathgeber says:

Hello - I must say, I’m impressed with your site. I had no trouble navigating through all the tabs and information was very easy to access. I found what I wanted in no time at all. Pretty awesome. Would appreciate it if you add forums or something, it would be a perfect way for your clients to interact. Great job

10.
Ivory Micklos Ivory Micklos United States says:

I'm so glad to have found your web page. My pal mentioned it to me before, yet never got around to checking it out until now. I must express, I'm floored. I really enjoyed reading through your posts and will absolutely be back to get more.

11.
Von Peeks Von Peeks United States says:

By far the most concise and up to date information I found on this topic. Sure glad that I navigated to your page by accident. I’ll be subscribing to your feed so that I can get the latest updates. Appreciate all the information here

Comments are closed