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: performance