Is using a Regular Expression faster than IndexOf?

Jack Marchetti picture Jack Marchetti · Feb 21, 2012 · Viewed 10.4k times · Source

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.

Answer

Guffa picture Guffa · Feb 21, 2012

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.