C# 点跟踪与索引优化方法

C# 点跟踪与索引优化方法,c#,performance-testing,dottrace,C#,Performance Testing,Dottrace,我使用的是dottrace,这个方法占用了我33%的CPU。我如何优化它。正因为如此,我的应用程序崩溃或内存不足。这种方法是静态的,聪明吗 dottrace显示此服务器上30%的cpu使用率: string[] result = null; result = HtmlHelper.GetStringInBetween(bits[0], bits[1], tagValuePair.Value, true, true); 编辑: getStringInBeween(字符串strBegin、字符串

我使用的是dottrace,这个方法占用了我33%的CPU。我如何优化它。正因为如此,我的应用程序崩溃或内存不足。这种方法是静态的,聪明吗

dottrace显示此服务器上30%的cpu使用率:

string[] result = null;
result = HtmlHelper.GetStringInBetween(bits[0], bits[1], tagValuePair.Value, true, true);

编辑:

getStringInBeween(字符串strBegin、字符串streng、字符串strSource、bool includeBegin、bool includeEnd)
strBegin=“”
strEnd=“”
strSource=“XXXXXXXX DI.31.01.12XXXXXXXXXXXXX
includeBegin=true
includeEnd=true
然后我会得到结果
结果[0]=“Di。31.01.12"

希望这有助于此方法的工作。尝试在strBegin和strengd之间查找字符串…

对于示例输入,您似乎正在处理HTML片段


我建议使用解析HTML—它以易于查询的方式公开结果,使用LINQ to XML或XPath类型语法。它是一个快速高效的库。

复制字符串的一部分(您的第一个子字符串调用)只在其中进行搜索对性能有害。相反,保留原始输入字符串,但在IndexOf上使用重载,该重载获取起始索引,然后调整索引计算以相应地提取结果

另外,知道这些字符串没有本地化,您可以通过在IndexOf中使用顺序比较器获得一些

类似于

    GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)

strBegin = "<td class=\"m92_t_col2\">"
strEnd = "</td>"
strSource = "xxxxxxxx<td class=\"m92_t_col2\">Di. 31.01.12</td>xxxxxxxxxxxxxx
includeBegin = true
includeEnd = true

then i will get result
result[0] = "<td class=\"m92_t_col2\">Di. 31.01.12</td>"
public static string[]GetStringInBetween(string strBegin、string streng、string strSource、bool includeBegin、bool includeEnd)
{
字符串[]结果={“”“”};
int iIndexOfBegin=strSource.IndexOf(strBegin,StringComparison.Ordinal);
如果(iIndexOfBegin!=-1)
{
intiend=strSource.IndexOf(strengd,iIndexOfBegin,StringComparison.Ordinal);
如果(iEnd!=-1)
{
结果[0]=strSource.Substring(
iIndexOfBegin+(包括边界?0:strBegin.Length),
iEnd+(包括强度长度:0)-iIndexOfBegin);
//超越这一部分
if(iEnd+强度长度
你考虑过使用正则表达式吗?@Oded我考虑过,但我对regex非常不好,所以我用indexof编写代码,我现在看到这是个坏主意……dottrace是否告诉你调用
System.String.indexof(String,Int32,Int32,StringComparison)
来自
GetStringInBetween
如果是,你能简单地减少对
GetStringInBetween
的调用吗?是的,它来自GetStringInBetween。我如何减少代码?我不知道正则表达式与它们所操作的文本有多大关系-如果不知道更多关于ki的信息,就不可能提出建议输入结束。我必须发布另一个答案,因为这里没有足够的空间。哦,等等,我可以编辑。
System.String.IndexOf(String, Int32, Int32, StringComparison)
    GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)

strBegin = "<td class=\"m92_t_col2\">"
strEnd = "</td>"
strSource = "xxxxxxxx<td class=\"m92_t_col2\">Di. 31.01.12</td>xxxxxxxxxxxxxx
includeBegin = true
includeEnd = true

then i will get result
result[0] = "<td class=\"m92_t_col2\">Di. 31.01.12</td>"
public static string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
{
    string[] result = { "", "" };
    int iIndexOfBegin = strSource.IndexOf(strBegin, StringComparison.Ordinal);

    if (iIndexOfBegin != -1)
    {
        int iEnd = strSource.IndexOf(strEnd, iIndexOfBegin, StringComparison.Ordinal);

        if (iEnd != -1)
        {
            result[0] = strSource.Substring(
                iIndexOfBegin + (includeBegin ? 0 : strBegin.Length), 
                iEnd + (includeEnd ? strEnd.Length : 0) - iIndexOfBegin);

            // advance beyond this segment
            if (iEnd + strEnd.Length < strSource.Length)
                result[1] = strSource.Substring(iEnd + strEnd.Length);
        }
    }

    return result;
}