Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performant way to compare array items to other array items

Tags:

arrays

c#

regex

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!

like image 661
Krusty40k Avatar asked Feb 11 '26 03:02

Krusty40k


1 Answers

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
        }
    }
}
like image 140
Sergey Kalinichenko Avatar answered Feb 15 '26 14:02

Sergey Kalinichenko