Ali
Ali

Reputation: 267077

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 different than the other? (That way I can leave the identical strings alone and process the ones that have changed).

Upvotes: 0

Views: 658

Answers (2)

ircmaxell
ircmaxell

Reputation: 165201

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).

Upvotes: 3

weltraumpirat
weltraumpirat

Reputation: 22604

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.

Upvotes: 2

Related Questions