Question

I have an app running which looks at items in a queue, then based upon certain keywords a category is applied - then it is inserted into a database.

I'm using IndexOf to determine if a certain keyword is present.

Is this the ideal way or would a RegEX be faster?

There's about 10 items per second being processed or so.

Was it helpful?

Solution

For just finding a keyword the IndexOf method is faster than using a regular expression. Regular expressions are powerful, but their power lies in flexibility, not raw speed. They don't beat string methods at simple string operations.

Anyway, if the strings are not huge, it shouldn't really matter as you are not doing it so often.

OTHER TIPS

http://ayende.com/blog/2930/regex-vs-string-indexof

It seems it may matter on the length of the string on efficiency.

The only way you know for sure is testing it. But making an educated guess it depends on the number of keywords your are testing, the length of the text, etc. The indexOf would probably win.

The only way you know for sure is write a test for your specific scenario.

I doubt it - indexOf is a very simple algorithm that will just seek through your string and return the first occurrence it finds.

Regex is a far more complex mechanism that needs to be parsed and checked against the whole string. If your string is very large, you are better off with indexOf.

First of all, with 10 items per second you probably don't even need to think about performance.

IndexOf is probably faster than regex in most cases. Especially if you don't use a precompiled regex.

It's performance might also depend on the chosen string comparison/culture. I expect StringComparison.Ordinal to be fastest.

Why not experiment and measure the time elapsed using the System.Diagnostics.Stopwatch class? http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

Set up a Stopwatch object before your indexOf operation and then measure elapsed time after it. Then, swap out the indexOf for a regular expression. Finally, report back with your findings so that we can see them too!

At least this programmer finds it faster to understand the code that uses IndexOf!

Does saving a little CPU time justify putting up the time it takes the next person to understand the code?

You can find information about this very query on this link: http://ayende.com/blog/2930/regex-vs-string-indexof

In summary it seems to indicate that the larger the searchpattern the better RegEx performs comparatively.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top