Is using a Regular Expression faster than IndexOf?

梦想的初衷 提交于 2019-12-22 03:45:05

问题


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.


回答1:


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.




回答2:


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

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




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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!




回答7:


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?




回答8:


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.



来源:https://stackoverflow.com/questions/9380062/is-using-a-regular-expression-faster-than-indexof

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