简体   繁体   English

如何最有效地检查两个字符串是否不同?

[英]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). (这样一来,我可以将相同的字符串保留下来,并处理已更改的字符串)。

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. 它对字符串进行逐位比较,因此应该在最差的 O(n)下进行,其中n是较小字符串的大小。 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). 我认为您没有比这更好的了(缺少跟踪字符串以及是否更改了字符串,但是问题的措辞方式似乎是您想要做的就是比较它们)。

您可以比较哈希值,也可以创建一个包装器类,其中包含有问题的字符串和一个“ changed”标志,该标志在每次更改字符串时都设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM