I'm writing a program where I want to check 16,000 strings for about 100 phrases.
My simple way of doing this were two for-loops:
(resulting in 1,600,000 string operations)
string[] phrases;
string[] texts;
for(int t_count = 0; t_count < 16000; t_count++)
{
for(int p_count = 0; p_count < 100; p_count++)
{
Regex pattern = new Regex(phrases[p_count]);
if (pattern.IsMatch(texts[t_count]))
{
//Save phrases[p_count]
break;
}
}
}
I think that there must be more effcient ways to do this.
Any suggestions are welcome.
EDIT: @ J. Steen Of course it shall run faster, but producing unicorns at the same time would be awesome!
Start by switching the order of the loops - rather than compiling each of the 100 regular expressions 16000 times, this would compile them once:
for(int p_count = 0; p_count < 100; p_count++)
{
Regex pattern = new Regex(phrases[p_count]);
for(int t_count = 0; t_count < 16000; t_count++)
{
if (pattern.IsMatch(texts[t_count]))
{
//Do Something
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With