如何超越这个正则表达式替换? [英] How to outperform this regex replacement?

查看:144
本文介绍了如何超越这个正则表达式替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可观测量后,我已经在我们的窗口服务,我想优化一个确定的热点。我们正在处理,可能有多个连续的空格在它的字符串,我们希望减少只有一个空格。我们使用静态编译的正则表达式完成这个任务:

After considerable measurement, I have identified a hotspot in one of our windows services that I'd like to optimize. We are processing strings that may have multiple consecutive spaces in it, and we'd like to reduce to only single spaces. We use a static compiled regex for this task:

private static readonly Regex 
    regex_select_all_multiple_whitespace_chars = 
        new Regex(@"\s+",RegexOptions.Compiled);



,然后用它如下:

and then use it as follows:

var cleanString=
    regex_select_all_multiple_whitespace_chars.Replace(dirtyString.Trim(), " ");

这行被调用数百万次,被证明是相当密集。我试着写一些东西更好,但我难倒。鉴于正则表达式的相当温和的加工要求,肯定有什么东西更快。莫非不安全的指针速度的东西进一步处理。

This line is being invoked several million times, and is proving to be fairly intensive. I've tried to write something better, but I'm stumped. Given the fairly modest processing requirements of the regex, surely there's something faster. Could unsafe processing with pointers speed things further?

编辑:

感谢惊人的一套应对这个问题...最意想不到的!

Thanks for the amazing set of responses to this question... most unexpected!

推荐答案

这是三倍左右速度快:

private static string RemoveDuplicateSpaces(string text) {
  StringBuilder b = new StringBuilder(text.Length);
  bool space = false;
  foreach (char c in text) {
    if (c == ' ') {
      if (!space) b.Append(c);
      space = true;
    } else {
      b.Append(c);
      space = false;
    }
  }
  return b.ToString();
}

这篇关于如何超越这个正则表达式替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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