As the title says, just looking for a string to match a client finishing sending data over a socket, so I might be looking for something like {"Message" : "END"}
in a JSON string for example.
A the most the strings will be a few hundred chars long.
5 Answers
They're both fast enough to be over before you know it. Better to go for the one that you can read more easily.
But from forums, blogs contains
is faster, but still negligible performance difference
-
2The important point is that both regex and
contains
are incorrect. It doesn't matter which incorrect procedure is faster than the other one.– TomalakJan 29, 2015 at 7:20 -
@Buffalo They are incorrect because they look at the unparsed JSON text. And as far as JSON is concerned,
{"Message" : "END"}
and{"\u004d\u0065\u0073\u0073\u0061\u0067\u0065": "\u0045\u004e\u0044"}
are exactly the same thing. The string-contains search will only find one of them. Real-world situations might be more subtle than that, but the point remains - JSON must be parsed before inspecting the contents. Not doing so is a bug waiting to happen.– TomalakNov 5, 2018 at 12:41
I had tried both approaches and repeated them over 100k times and String.contains()
is a lot faster than Regex.
However String.Contains()
is only useful for checking the existence of an exact substring, whereas Regex allows you to do more wonders. So it depends.
You can test it yourself by creating benchmark using Caliper - Google's open-source framework
Read more about What is a microbenchmark?
From How to use regex in String.contains() method in Java
String.contains
String.contains
works with String, period. It doesn't work with regex. It will check whether the exact String specified appear in the current String or not.Note that
String.contains
does not check for word boundary; it simply checks for substring.
Its performance is good it will take fraction of less seconds then Regex.
Regex solution
Regex is more powerful than
String.contains
, since you can enforce word boundary on the keywords (among other things). This means you can search for the keywords as words, rather than just substrings.
So it is taking more time of execution to parse whole string.
-
1I don't know why you are quoting from my post, since the other post doesn't say anything about the performance.– nhahtdhJan 29, 2015 at 8:25
-
@nhahtdh In your post you explained very well about
String.contains
and Regex, and I thought it's better to provide some explanation with , why explanation of whyString.contains
is faster thenRegex
. Please comment if you want I delete my post. Jan 29, 2015 at 11:13 -
1
String.contains
is not always faster than regex. For normal cases,contains
is faster. However, in the worst case (likeaaaaaab
onaaaaabaaaaabaaaaabaaaaab
...), depending on the regex implementation, it may uses advanced string matching algorithm, which guarantees linear time complexity. In such cases, the simpleindexOf
implementation will have quadratic time complexity.– nhahtdhJan 29, 2015 at 12:13 -
1You can keep the quotes, but don't suggest that my quotes lead to your claims of the performances. I don't want people to think that I mentioned about the performance in the other post.– nhahtdhJan 29, 2015 at 12:15
To determine which is the fastest you will have to benchmark your own system. However, regular expressions are complex and chances are that String.Contains()
will be the fastest and in your case also the simplest solution.
The implementation of String.Contains()
will eventually call the native method IndexOfString() and the implementation of that is only known by Microsoft. However, a good algorithm for implementing this method is using what is known as the Knuth–Morris–Pratt algorithm. The complexity of this algorithm is O(m + n) where m is the length of the string you are searching for and n is the length of the string you are searching making it a very efficient algorithm.
Actually, the efficiency of search using regular expression can be as low O(n) depending on the implementation so it may still be competetive in some situations. Only a benchmark will be able to determine this.
.contains
if you're just looking for text.)