【问题标题】:dottrace and optimizing method with indexofdottrace 和 indexof 的优化方法
【发布时间】:2012-01-26 18:07:45
【问题描述】:
public static string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
        {
            string[] result = { "", "" };
            int iIndexOfBegin = strSource.IndexOf(strBegin);

            if (iIndexOfBegin != -1)
            {
                // include the Begin string if desired
                if (includeBegin)
                    iIndexOfBegin -= strBegin.Length;

                strSource = strSource.Substring(iIndexOfBegin + strBegin.Length);
                int iEnd = strSource.IndexOf(strEnd);

                if (iEnd != -1)
                {
                    // include the End string if desired
                    if (includeEnd)
                        iEnd += strEnd.Length;

                    result[0] = strSource.Substring(0, iEnd);

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

            return result;
        }

用法:

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

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

dottrace 显示 30% 的 cpu 使用率:

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>"

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

【问题讨论】:

  • 您是否考虑过为此使用正则表达式?
  • @Oded 我做到了,但我对正则表达式非常糟糕,所以我用 indexof 编写代码,我现在认为这是个坏主意......
  • dottrace 是否告诉您对System.String.IndexOf(String, Int32, Int32, StringComparison) 的调用来自GetStringInBetween 如果是,您可以简单地减少对GetStringInBetween 的调用
  • 是的,它来自 GetStringInBetween。我怎样才能减少代码?我不太了解正则表达式
  • 正则表达式与它们操作的文本密切相关 - 如果不了解更多关于输入类型的信息,就不可能提出建议。

标签: c# performance-testing dottrace


【解决方案1】:

复制字符串的一部分(您的第一次 SubString 调用)只是为了继续搜索,这对性能不利。相反,保留您的原始输入字符串,但使用 IndexOf 上的重载,该重载需要一个起始索引,然后调整您的索引计算以相应地提取结果。

此外,知道这些字符串未本地化,您可能会通过在 IndexOf 中使用序数比较器来获得一些好处。

类似的东西

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;
}

【讨论】:

  • 我将不得不发布另一个答案,因为这里没有足够的空间。哦,等等 - 我可以编辑。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2020-06-01
    • 1970-01-01
    • 2021-09-24
    • 2012-05-07
    • 1970-01-01
    相关资源
    最近更新 更多