Is String.Contains () faster than traversing the entire char array in a string?

I have a function going through a line that searches for a template and changes its parts. I could optimize it by inserting

if (!text.Contains(pattern)) return;

But I really go through the entire line and compare its parts with the template, so the question is, how does it work String.Contains()? I know there was such a question - How does String.Contains work? but the answer is rather unclear. So, if String.Contains()looking through the entire array of characters and comparing them with the template I'm looking for, it would not make my function faster, but slower.

So, is it a good idea to try such an optimization? And - is it possible that it String.Contains()will be even faster than a function that just goes through the entire array and compares each individual character with some constant?

Here is the code:

    public static char colorchar = (char)3;

    public static Client.RichTBox.ContentText color(string text, Client.RichTBox SBAB)
    {
        if (text.Contains(colorchar.ToString()))
        {
            int color = 0;
            bool closed = false;
            int position = 0;
            while (text.Length > position)
            {
                if (text[position] == colorchar)
                {
                    if (closed)
                    {
                        text = text.Substring(position, text.Length - position);
                        Client.RichTBox.ContentText Link = new Client.RichTBox.ContentText(ProtocolIrc.decode_text(text), SBAB, Configuration.CurrentSkin.mrcl[color]);
                        return Link;
                    }

                    if (!closed)
                    {
                        if (!int.TryParse(text[position + 1].ToString() + text[position + 2].ToString(), out color))
                        {
                            if (!int.TryParse(text[position + 1].ToString(), out color))
                            {
                                color = 0;
                            }
                        }
                        if (color > 9)
                        {
                            text = text.Remove(position, 3);
                        }
                        else
                        {
                            text = text.Remove(position, 2);
                        }
                        closed = true;
                        if (color < 16)
                        {
                            text = text.Substring(position);
                            break;
                        }
                    }
                }
                position++;
            }
        }
        return null;
    }
+5
source share
6 answers

If string.Contains is not fast enough, then it is unlikely that any optimization of the equivalent method will be fast enough.

First, are you sure that string.Contains is actually not fast enough? I find it hard to believe if you are not doing massive string processing.

Secondly, if you are sure of this, you need to look at another way to achieve your goal, perhaps using some kind of indexing utility such as Lucene.Net

+3
source

, .
, String.Contains(...) String.IndexOf(..) >= 0
, :

int position = text.IndexOf(colorchar.ToString()...);
if (-1 < position)
{  /* Do it */ }
+1

.

(ahhm...).

, String.Contains( IndexOf).

, IIRC String.Contains .Net

, ,

0

string.contains

, - , String.Contains, CRT wcsstr, msvcrt.dll,

0

, String.Contains , . , .

.NET, , . , () .NET x86. , , #.

If you really want to make sure your custom string comparison code is faster than String.Containsyou need to measure them as using a lot of iterations, each with a different string. For example, using a Stopwatchclass to measure time.

0
source

If you now have details that you can use for optimization (and not just just contain a check), make sure you can make your method faster than string.Contains, otherwise not.

0
source

All Articles