We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,324 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

What could be faster than .IndexOf()

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());
5
Contributors
6
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
7
Views
Question
Answered
Darth Vader
Junior Poster in Training
94 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

Momerath
Senior Poster
3,734 posts since Aug 2010
Reputation Points: 1,336
Solved Threads: 624
Skill Endorsements: 13

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?

Darth Vader
Junior Poster in Training
94 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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);
    }
tinstaafl
Nearly a Posting Virtuoso
1,470 posts since Jun 2010
Reputation Points: 429
Solved Threads: 262
Skill Endorsements: 14

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.

TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 149
Skill Endorsements: 13

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!

Darth Vader
Junior Poster in Training
94 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by tinstaafl, Momerath and TnTinMN

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!

mwatson81
Newbie Poster
7 posts since Sep 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1021 seconds using 2.84MB