How to outperform this regex replacement?
- by spender
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?