Is using a Regular Expression faster than IndexOf?

≡放荡痞女 提交于 2019-12-05 02:54:27

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!