0

I have performance profiled the application for a long time

It turns out that the most of the CPU is used by the IndexOf function

enter image description here

Here the function

public static string func_Fix_Google_Source(string srSource)
{
    int irIndex = srSource.IndexOf("<div id=\"gt-form-c\">");
    return srSource.Substring(irIndex);
}

The length of the string would matter? I may shorten the seek string length i believe

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

2 Answers2

2

If substring searching becomes a bottleneck, you can switch to an advanced algorithm, such as KMP.

The default string search may be very expensive when you have lots of "false positives", e.g. lots of

<div id="...

in the text being searched.

Every time the prefix above is discovered at a position where a match does not start, the default algorithm follows it for as much as it takes to confirm the absence of match, and then it goes to the next position. In contrast, KMP skips many characters that it has already seen, improving the efficiency.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

In addition to improving the way you are searching, which it looks like you may have already done going by your comments, the IndexOf(string) overload uses the current culture to do its comparisons.

Using the StringComparision overload to do an Ordinal search will give better performance. Depending on what you are doing ~8x.

int irIndex = srSource.IndexOf("<div id=\"gt-form-c\">", StringComparison.Ordinal);
Zoom
  • 401
  • 5
  • 9