Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 执行将数组项与其他数组项进行比较的方法_C#_Arrays_Regex - Fatal编程技术网

C# 执行将数组项与其他数组项进行比较的方法

C# 执行将数组项与其他数组项进行比较的方法,c#,arrays,regex,C#,Arrays,Regex,我正在编写一个程序,其中我想检查16000个字符串中大约100个短语。 我的简单方法是两个for循环: (导致1600000个字符串操作) string[]短语; 字符串[]文本; 对于(int t_count=0;t_count

我正在编写一个程序,其中我想检查16000个字符串中大约100个短语。
我的简单方法是两个for循环:
(导致1600000个字符串操作)

string[]短语;
字符串[]文本;
对于(int t_count=0;t_count<16000;t_count++)
{
对于(int p_count=0;p_count<100;p_count++)
{
正则表达式模式=新正则表达式(短语[p_count]);
if(pattern.IsMatch(文本[t_count]))
{
//保存短语[p_count]
打破
}
}
}
我认为必须有更有效的方法来做到这一点。
欢迎提出任何建议

编辑:@J.Steen
当然,它会跑得更快,但同时生产独角兽会很棒

首先切换循环的顺序,而不是编译100个正则表达式中的每一个16000次,这将编译它们一次:

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
        }
    }
}
for(int p_count=0;p_count<100;p_count++)
{
正则表达式模式=新正则表达式(短语[p_count]);
对于(int t_count=0;t_count<16000;t_count++)
{
if(pattern.IsMatch(文本[t_count]))
{
//做点什么
}
}
}

效率如何?时间记忆?独角兽?@J.Steen Unicorns是的!没有更多的细节,我们无法帮助改进它。也许您可以使用纯字符串方法,而不是正则表达式或循环中的某个
break
。但是谁知道呢?例如,缓存
Regex
(es)将是一个开始,并提供一些短语示例。还有一个//do something的例子有助于更好地回答问题,可能是
RegexOptions。Compiled
也会很有用,但您必须对其进行分析才能确定。@xantos No,显示默认值为
None
。如果每个不同的regex只构造一次,则留下RegexOptions。编译应该更快,因为编译设置会给构造函数增加初始化时间。
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
        }
    }
}