How to outperform this regex replacement?

c#, optimization, regex, string

Solution

This is about three times faster:

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();
}

Problem

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? Edit: Thanks for the amazing set of responses to this question... most unexpected!

Original source