How to most efficiently check if two strings are different?
I have two very large strings. How can I compare them to tell if they're identical, or if one of them is 开发者_如何学Cdifferent than the other? (That way I can leave the identical strings alone and process the ones that have changed).
The most efficient way is to do:
$string1 == $string2
It does a bit-wise comparison of the strings, so should be at worst O(n)
where n
is the size of the smaller string. I don't think you're going to get much better than that (short of keeping track of the strings and if they were changed, but the way your question is worded it seems like all you want to do is compare them).
You could compare hash values, or create a wrapper class containing the string in question and a "changed" flag that is set to true
each time the string is altered.
精彩评论