I have a list of approx. 500,000 strings, each approx. 100 characters long. Given a search term, I want to identify all strings in the list that contain the search term. At the moment I am doing this with a plain old dataset using the Select method ("MATCH %term%"). This takes about 600ms on my laptop. I'd like to make it faster, maybe 100-200ms.
What would be a recommended approach?
Performance is critical so I can trade memory footprint for better performance if necessary (within reason). The list of strings will not change once initialised so calculating hashes would also be an option.
Does anyone have a recommendation and which C# data structures are best suited to the task?
A trie or suffix tree would help in making this faster - this is essentially what fulltext search (usually) is using.
There are implementations in C# you can use, also see this SO thread: Looking for the suffix tree implementation in C#?
Also as mentioned by @leppie parallel execution will likely be already provide you with the x3 performance gain you are looking for. But then again you will have to measure closely, without that it's anyone's guess.
Have you tried loading your strings into a
List<string>
and then using the Linq extensionsContains
method?According to these benchmarks, the fastest way to check if a string occurs in a string is the following:
Thus, you could:
Obviously you'd have to adapt them to your List[string] (or whatever data structure you're using).
I've heard good things about Lucene.NET when it comes to performing quick full-text searches. They've done the work to figure out the fastest data structures and such to use. I'd suggest giving that a shot.
Otherwise, you might just try something like this:
But it probably won't get you down to 100ms.
You should try to use Dictionary class. It's much faster than List because it's an indexed search.
Have you tried the following?
For some reason the List.AsParallel().Where(...) is slower than list.FindAll(...) on my PC.
Hope this will help you.