问题
I have a CLR function to replace string values. but it runs a long time. How can I improve this function and make it runs faster?
public static SqlString ReplaceValues(String Value)
{
// Put your code here
char[] c = new char[] { '.' };
Value= Value.Trim();
Value = Value.Trim(c);
Value = Value.Replace("'", "").Replace(")", " ").Replace("(", " ").Replace("-", " ").Replace("_", " ");
Value = Regex.Replace(Value, @"[י", "+[י");
Value = Regex.Replace(Value, @"\s+", " ");
return new SqlString(Value.Trim());
}
EDIT: I changed my function to use value.Replace, it's better, but still it runs more time than expected:
public static SqlString ReplaceStreetValues(String Value)
{
// Put your code here
Value = Value.Trim();
char[] c = new char[]{'.'};
Value = Value.Trim(c);
Value= Value.Replace("'", "").Replace(")", " ").Replace("(", " ").Replace("-", " ").Replace("_", " ").Replace("רח", "");
while (Value.IndexOf(" ")!=-1)
Value = Value.Replace(" ", " ");
while (Value.IndexOf("hh") !=-1)
Value = Value.Replace("hh", "h");
return new SqlString(Value.Trim());
}
thanks!!!
回答1:
Try to use StringBuilder.Replace instead.
Should notably improve performance.
This is valid like a string.Replace(..)
substitude and not for regex
calls.
But apparently the bottleneck is in string
calls.
EDIT:
Example (pesudocode):
char[] c = new char[]{'.', ' '};
Value = Value.Trim(c);
var sb = new StringBuilder(Value);
sb.Replace("'", "");
sb.Replace(")", " ");
sb.Replace("(", " ");
sb.Replace("-", " ");
sb.Replace("_", " ");
sb.Replace("רח", "");
来源:https://stackoverflow.com/questions/11069373/how-to-improve-the-replace-function-in-clr-function