Hello,

I wonder if there is any faster method to find an index of an occurence within a string?
The .IndexOf method in the below test, takes approx: 1050 milliseconds.

Why this is interesting is that I have code that uses .IndexOf on perheps 100 places
and have loops that could exeed billions of iterations.

Thank you!

                //Approx: 1050 milliseconds
                DateTime dt = DateTime.Now;
                String str = " hello hello.. ,0";
                int loops = 4250000;
                for (int i = 0; i < loops; i++)
                {
                    index = str.IndexOf(",", 0);
                }
                DateTime dt2 = DateTime.Now;
                TimeSpan diff = dt2 - dt;

                MessageBox.Show(diff.TotalMilliseconds.ToString());

Recommended Answers

All 6 Replies

There are many different string searching algorithms. Depending on what you are searching for, how long the strings are, etc. will effect the efficiency of these algorithms.

So, what are you searching for? Are you searching the same string multiple times? How long is the search string? How long is the string you are searching for? Are you trying to find all occurances or just the first?

The example would be exactly as my example goes, so it is very basic in this case.
The string is the size of the example and it is the first occurence "," in the string to find indexof.

You could go with my example as it is. Then how to make that faster if possible?

Did some playing around and found putting the search into a separate function increased the speed by a factor of approximately 4(i.e. on my machine original code was 1200-1300, the new code
300-400)

        //Approx: 1050 milliseconds
        DateTime dt = DateTime.Now;
        String str = " hello hello.. ,0";
        int index = 0;
        int loops = 4250000;
        for (int i = 0; i < loops; i++)
        {
            index =  Find(str, ","[0]);
        }
        DateTime dt2 = DateTime.Now;
        TimeSpan diff = dt2 - dt;
        MessageBox.Show(diff.TotalMilliseconds.ToString());
    }
    public static int Find(string Search, char Term)
    {
        return Search.IndexOf(Term);
    }

Did some playing around and found putting the search into a separate function increased the speed by a factor of approximately 4(i.e. on my machine original code was 1200-1300, the new code
300-400)
You also switched to using a char for the seek parameter versus char which is the the reason for the speed increase not the fact that it hidden under another function.

If the OP would change: str.IndexOf(",", 0) to str.IndexOf(',', 0) the factor of 4 performance boost would also be observed.

So there is a lesson in this; when seeking for one char use the char overload not the string overload.

If the OP would change: str.IndexOf(",", 0) to str.IndexOf(',', 0) the factor of 4 performance boost would also be observed.

This was the best news I have heard. I actually didn´t know this at all.
The performance boost is about 4 times greater here also.

Thank you for this help!

Searching for a single char like that versus a string is probably a good tip for a lot of string type functions. Learned something new! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.