How to improve the Replace function in CLR function?

丶灬走出姿态 提交于 2020-03-28 06:15:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!