Windows Server 2008上的正则表达式放缓 [英] Regex slow on Windows Server 2008

查看:86
本文介绍了Windows Server 2008上的正则表达式放缓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我经常EX pressions编译极为缓慢的Windows Server 2008上我写了一个小控制台应用程序来突出这一问题。该应用程序生成自己的投入,并建立从词的正则表达式在一个XML文件中。我建立了这个应用程序的发布版本和我个人的笔记本电脑(运行XP)和Windows 2008服务器上运行它两个。定期EX pression了0.21秒编译我的笔记本电脑,但23秒,编译服务器上。

任何想法可能会导致什么呢?问题是,只有在第一次使用正则表达式的(当它第一次编译 - 然后它是好的)

我还发现了另外一个问题 - 在同一个Windows 2008服务器上的普通恩pression使用\ S +时,内存气球(使用4GB +)和正则表达式的编译无法完成

有一个已知的问题与正则表达式位和64位。NET?是否有一个修复/补丁程序适用于此?我真的不能在网络上找到任何信息,但我发现这个同样的问题几篇文章中框架2.0 - ?想必这已得到修复现在

更多信息: 服务器运行的是64位版本的.NET Framework(3.5 SP1)和我的笔记本电脑我有Visual Studio 2008和安装了3.5框架。定期EX pression是以下模式:^字$ | ^字$ | ^ $字,并具有下列标志构建:RegexOptions.IgnoreCase | RegexOptions.Compiled

编辑: 这里是一个code片断:

  StringBuilder的regexString =新的StringBuilder();
如果(!String.IsNullOrEmpty(fileLocation))
{
    XmlTextReader的的TextReader =新的XmlTextReader(fileLocation);
    textReader.Read();
    而(textReader.Read())
    {
        textReader.MoveToElement();
        如果(textReader.Name ==字)
        {
            regexString.Append(^+ textReader.GetAttribute(0)+$ |);
        }
    }
    ProfanityFilter =新的正则表达式(regexString.ToString(0,regexString.Length  -  1),RegexOptions.IgnoreCase | RegexOptions.Compiled);
}

日期时间时间= DateTime.Now;
Console.WriteLine(\ nIsProfane:\ N+ ProfanityFilter.IsMatch(测试));
Console.WriteLine(\ n时间:+(DateTime.Now  - 时间).TotalSeconds);
Console.ReadKey();
 

这导致了0.21秒,我的笔记本电脑和23秒2008服务器上的时间。 XML文件包含168字的格式如下:

 <文字文本=测试/>
 

解决方案

我找到了一个解决办法,因为不正确的,但完美在我的情况。出于某种原因,如果我离开了 RegexOptions.Compiled 标记,在正则表达式是更多,更快。我甚至设法在2008年服务器上在65毫秒执行正则表达式 100长的短语在

这必须是在.NET LIB一个错误的未编译的版本应该是比编译的版本要慢得多。无论哪种方式,每个检查下1毫秒是非常可以接受的对我来说:)

I have a situation where my regular expressions compile extremely slowly on Windows Server 2008. I wrote a small console application to highlight this issue. The app generates its own input and builds up a Regex from words in an XML file. I built a release version of this app and ran it both on my personal laptop (running XP) and the Windows 2008 server. The regular expression took 0.21 seconds to compile on my laptop, but 23 seconds to compile on the server.

Any ideas what could be causing this? The problem is only on first use of the Regex (when it is first compiled - thereafter it is fine)

I have also found another problem - when using \s+ in the regular expression on the same Windows 2008 server, the memory balloons (uses 4GB+) and the compilation of the Regex never finishes.

Is there a known issue with Regex and 64 bit .net? Is there a fix/patch available for this? I cannot really find any info on the net, but I have found a few articles about this same issues in Framework 2.0 - surely this has been fixed by now?

More info: The server is running the 64 bit version of the .net framework (3.5 SP1) and on my laptop I have Visual Studio 2008 and the 3.5 framework installed. The regular expression is of the following pattern: ^word$|^word$|^word$ and is constructed with the following flags: RegexOptions.IgnoreCase | RegexOptions.Compiled

Edit: Here is a code snippet:

StringBuilder regexString = new StringBuilder();
if (!String.IsNullOrEmpty(fileLocation))
{
    XmlTextReader textReader = new XmlTextReader(fileLocation);
    textReader.Read();
    while (textReader.Read())
    {
        textReader.MoveToElement();
        if (textReader.Name == "word")
        {
            regexString.Append("^" + textReader.GetAttribute(0) + "$|");
        }
    }
    ProfanityFilter = new Regex(regexString.ToString(0, regexString.Length - 1), RegexOptions.IgnoreCase | RegexOptions.Compiled);
}

DateTime time = DateTime.Now;
Console.WriteLine("\nIsProfane:\n" + ProfanityFilter.IsMatch("test"));
Console.WriteLine("\nTime: " + (DateTime.Now - time).TotalSeconds);
Console.ReadKey();

This results in a time of 0.21 seconds on my laptop and 23 seconds on the 2008 server. The XML file consists of 168 words in the following format:

<word text="test" />

解决方案

I found a solution, given not the correct one, but perfect in my case. For some reason if I leave out the RegexOptions.Compiled flag, the Regex is much, much faster. I even managed to execute the Regex on 100 long phrases in under 65 milliseconds on the 2008 server.

This must be a bug in the .net lib as the uncompiled version is supposed to be much slower than the compiled version. Either way, under 1 millisecond per check is very much acceptable for me :)

这篇关于Windows Server 2008上的正则表达式放缓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆