如何改进CLR功能中的Replace功能? [英] How to improve the Replace function in CLR function?

查看:42
本文介绍了如何改进CLR功能中的Replace功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CLR 函数来替换字符串值.但它运行了很长时间.如何改进这个功能,让它运行得更快?

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

   }

我将我的函数更改为使用 value.Replace,它更好,但它的运行时间仍然比预期的要长:

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

    }

谢谢!!!

推荐答案

尝试使用 StringBuilder.Replace 代替.

应该显着提高性能.

这与 string.Replace(..) 替代一样有效,但不适用于 regex 调用.但显然瓶颈在于 string 调用.

This is valid like a string.Replace(..) substitude and not for regex calls. But apparently the bottleneck is in string calls.

编辑:

示例(伪代码):

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("רח", "");

这篇关于如何改进CLR功能中的Replace功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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