C#性能优化: 正则表达式

.Net技术 码拜 12年前 (2015-01-23) 4109次浏览 0个评论

不要迷信正则表达式

正好在第一个例子里说到了正在表达式(Regex)对象就顺便一起说了。

很多人以为正则表达式很快,非常快,超级的快。

虽然正则表达式是挺快的,不过千万不要迷信他,不信你看下面的例子。

  1. //方法1
  2. public static string ConvertQuot1(string html)
  3. {
  4.     return html.Replace(“"”, “\””).Replace(“"”, “\””);
  5. }
  6. readonly static Regex ReplaceQuot = new Regex(“&(quot|#34);”, RegexOptions.IgnoreCase | RegexOptions.Compiled);
  7. //方法2
  8. public static string ConvertQuot2(string html)
  9. {
  10.     return ReplaceQuot.Replace(html, “\””);
  11. }

有多少人认为正则表达式比较快的,举个手??

C#性能优化: 正则表达式

结果为10w次循环的时间 ,即使是10个Replace连用,也比Regex好,所以不要迷信他。

  1. //方法1
  2. public static string ConvertQuot1(string html)
  3. {
  4.     return html.Replace(“0”, “”).Replace(“1”, “”).Replace(“2”, “”).Replace(“3”, “”).Replace(“4”, “”).Replace(“5”, “”).Replace(“6”, “”).Replace(“7”, “”).Replace(“8”, “”).Replace(“9”, “”);
  5. }
  6. readonly static Regex ReplaceQuot = new Regex(“[1234567890]”, RegexOptions.IgnoreCase | RegexOptions.Compiled);
  7. //方法2
  8. public static string ConvertQuot2(string html)
  9. {
  10.     return ReplaceQuot.Replace(html, “”);
  11. }

ConvertQuot1:3518

ConvertQuot2:12479

最后给你们看一个真实的,杯具的栗子。

  1. Htmlstring = Regex.Replace(Htmlstring, @“<(.[^>]*)>”, “”, RegexOptions.IgnoreCase);
  2. Htmlstring = Regex.Replace(Htmlstring, @“([\r\n])[\s]+”, “”, RegexOptions.IgnoreCase);
  3. Htmlstring = Regex.Replace(Htmlstring, @“–>”, “”, RegexOptions.IgnoreCase);
  4. Htmlstring = Regex.Replace(Htmlstring, @“<!–.*”, “”, RegexOptions.IgnoreCase);
  5. Htmlstring = Regex.Replace(Htmlstring, @“&(quot|#34);”, “\””, RegexOptions.IgnoreCase);
  6. Htmlstring = Regex.Replace(Htmlstring, @“&(amp|#38);”, “&”, RegexOptions.IgnoreCase);
  7. Htmlstring = Regex.Replace(Htmlstring, @“&(lt|#60);”, “<“, RegexOptions.IgnoreCase);
  8. Htmlstring = Regex.Replace(Htmlstring, @“&(gt|#62);”, “>”, RegexOptions.IgnoreCase);
  9. Htmlstring = Regex.Replace(Htmlstring, @“&(nbsp|#160);”, ” “, RegexOptions.IgnoreCase);
  10. Htmlstring = Regex.Replace(Htmlstring, @“&(iexcl|#161);”, “\xa1”, RegexOptions.IgnoreCase);
  11. Htmlstring = Regex.Replace(Htmlstring, @“&(cent|#162);”, “\xa2”, RegexOptions.IgnoreCase);
  12. Htmlstring = Regex.Replace(Htmlstring, @“&(pound|#163);”, “\xa3”, RegexOptions.IgnoreCase);
  13. Htmlstring = Regex.Replace(Htmlstring, @“&(copy|#169);”, “\xa9”, RegexOptions.IgnoreCase);
  14. Htmlstring = Regex.Replace(Htmlstring, @“&#(\d+);”, “”, RegexOptions.IgnoreCase);

合理使用正则表达式

上面说了正则表达式的效率不高,并不是说就不要用他了,至少正则表达式的作用不仅仅如此而已。

如果一定要用正则表达式的话也需要注意,能静态全局公用的尽量全局公用。

  1. readonly static Regex regex = new Regex(“[1234567890]”, RegexOptions.Compiled);

意他的第二个参数RegexOptions.Compiled 注释是 指定将正则表达式编译为程序集。这会产生更快的执行速度,但会增加启动时间。

通俗的说就是加了这个枚举,会使得初始化Regex对象变慢,但是执行字符串查找的时候更快, 不使用的话,初始化很多,查询比较慢。

之前测过相差蛮大的 ,代码就不比较了,有兴趣的可以自己试试相差多少。

另外还有一些枚举项,不确定是否对性能有影响,不过还是按规则使用会比较好。

  • RegexOptions.IgnoreCase // 指定不区分大小写的匹配, 如果表达式中没有字母,则不需要设定
  • RegexOptions.Multiline // 多行模式。更改 ^ 和 $ 的含义…. 如果表达式中没有^和$,则不需要设定
  • RegexOptions.Singleline // 指定单行模式。更改点 (.) 的含义…. 如果表达式中没有.,则不需要设定

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#性能优化: 正则表达式!
喜欢 (0)赏
[1034331897@qq.com]
分享 (0)

文章评论已关闭!