C# - 这是更快:String.Contains()或Regex.isMatch()? [英] C# - Which is faster: String.Contains() or Regex.isMatch()?
问题描述
这是更快,preferable为什么?
什么两者之间的机制有什么区别?
我要寻找从用户代理的一些值,最值,可以不带通配符(如使用,如果我想赶上手机我,而不是搜索 iPhone
* iPhone *
通配符)。
什么是快
尝试测量。但是,这是的错误的问题的,见下文。
preferable
如果我想匹配一个固定的字符串 String.Contains
不正是我需要的。如果我需要的模式匹配,则 String.Contains
是没用的。
比较这些性能是无关紧要的,他们这样做完全不同的事情。第一次使用正确的工具,也只有这样,如果你的表现是一个问题,使用分析,以确定您的code热部件来看待。
Possible Duplicate:
Regex.IsMatch vs string.Contains
Which is faster, preferable and why?
What the difference in mechanisms between two?
I need to search for some values from UserAgent, most of values can be used without wildcards (e.g. if I want to catch cellular phones I search for iPhone
instead of *iPhone*
wildcards).
What is faster
Try measuring. But this is the wrong question, see below.
preferable
If I want to match a fixed string String.Contains
does just what I need. If I need to pattern match, then String.Contains
is useless.
Comparing the performance of these is irrelevant, they do completely different things. Use the right tool first, and only then if your performance is a problem use profiling to identify hot parts of your code to look at.
这篇关于C# - 这是更快:String.Contains()或Regex.isMatch()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!