gpt4 book ai didi

c# - 在 .NET 中不同运行时间执行相同的正则表达式

转载 作者:行者123 更新时间:2023-11-30 21:53:40 34 4
gpt4 key购买 nike

我正在从事一个大量使用正则表达式的项目。我使用的正则表达式非常复杂,我必须设置适当的超时来停止执行,这样它就不会长时间尝试匹配字符串。

问题是我注意到在同一字符串上运行相同的正则表达式(已编译)正在以不同的运行时间执行,从 17 毫秒到 59 毫秒不等。

你知道为什么会这样吗?我正在使用 Stopwatch 测量运行时间,如下所示:

for (int i = 0; i < 15; i++)
{
sw.Start();
regex.IsMatch(message);
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds);
sw.Reset();
}

作为引用,我在 System.Text.RegularExpressions 中使用 .NET 中的默认正则表达式库。


根据评论,我修改了代码如下:

List<long> results = new List<long>(); 
for (int i = 0; i < 150; i++)
{
sw.Start();
for (int j = 0; j < 20; j++ )
{
regex.IsMatch(message);
}
sw.Stop();
results.Add(sw.ElapsedMilliseconds);
sw.Reset();
}
Debug.WriteLine(results.Max());
Debug.WriteLine(results.Average());
Debug.WriteLine(results.Min());

这个输出是:

790
469,086666666667
357

对我来说,差异仍然非常显着。

最佳答案

既然你说你正在使用 RegexOptions.Compiled,请引用 David Gutierrez's blog 中的正则表达式性能提示:

In this case, we first do the work to parse into opcodes. Then we also do more work to turn those opcodes into actual IL using Reflection.Emit. As you can imagine, this mode trades increased startup time for quicker runtime: in practice, compilation takes about an order of magnitude longer to startup, but yields 30% better runtime performance. There are even more costs for compilation that should mentioned, however. Emitting IL with Reflection.Emit loads a lot of code and uses a lot of memory, and that's not memory that you'll ever get back... The bottom line is that you should only use this mode for a finite set of expressions which you know will be used repeatedly.

这意味着第一次运行 regex 匹配时,将执行此额外工作(“编译时”),并且在没有该准备的情况下执行 regex 的所有后续时间。

但是,从 .NET 2.0 开始,behavior of caching has modified a bit :

In the .NET Framework 2.0, only regular expressions used in static method calls are cached. By default, the last 15 regular expressions are cached, although the size of the cache can be adjusted by setting the value of the CacheSize property.

关于c# - 在 .NET 中不同运行时间执行相同的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732032/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com