1,105,391 Community Members

What could be faster than .IndexOf()

Member Avatar
Darth Vader
Junior Poster in Training
97 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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());
Member Avatar
Momerath
Senior Poster
3,831 posts since Aug 2010
Reputation Points: 1,327 [?]
Q&As Helped to Solve: 664 [?]
Skill Endorsements: 19 [?]
Featured
 
0
 

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?

Member Avatar
Darth Vader
Junior Poster in Training
97 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 36 [?]
 
0
 

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);
    }
Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
2
 

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.

Member Avatar
Darth Vader
Junior Poster in Training
97 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Question Answered as of 1 Year Ago by tinstaafl, Momerath and TnTinMN
Member Avatar
mwatson81
Newbie Poster
7 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article