I was actually optimizing Regex transformations of large strings. As some calls to Regex.Replace took significant time I inserted conditional calls – something along the lines
if (str.IndexOf("abc", 0, StringComparison.CurrentCultureIgnoreCase) > 0)
str1 = Regex.Replace(str, "abc...", string.Empty, RegexOptions.IgnoreCase);
To my surprise the results were not convincing. Sometimes better, sometimes not. Or even worse. So I measured performance of case insensitive IndexOf (I tried also StringComparison.OrdinalIgnoreCase) and found that it may be slower than Regex.Match, i.e. this test:
if( Regex.Match(str,"abc", RegexOptions.IgnoreCase).Success )...
Particularly for a large string that did not match (ascii) string “abc” I found that Regex.Match was 4x faster.
Even this procedure is faster than IndexOf:
string s1 = str.ToLower();
if( s1.IndexOf("abc") ) ...
Anybody knows a better solution?
Because indexOf is O(n*m) where RegEx will likley be O(n+m) (where n= string length, m= search string length).
If you are serious ablut searching substrings it would be useful to read about string search algorithms to be at least aware of expected speeds (start with http://en.wikipedia.org/wiki/Substring_search ).
Note: Culture sensetive comparison may be significantly slower than Ordinal (depending on your scenario you may not be able to use Ordinal versions).
As with any performance questions one need to get out and measure. In indexOf with Regex.isMatch clear winner for me is Regex. I did expected the behavior as indexOf should not perform any pre-compile of the search string and have to use O(n+m) algorithm, while Regex have to use much more optimal implemetation.
Try to measure following searches – I get almost 5x difference in favor of Regex for 100K operations.