In a java app, assuming I have option of choosing the following comparison methods
在Java应用程序中,假设我可以选择以下比较方法
equalsIgnoreCase(String anotherString)
compareToIgnoreCase(String str)
Which one is faster?
哪一个更快?
3 个解决方案
#1
equalsIgnoreCase
can be a lot faster. For example, consider two strings which start with the same 10,000 characters - but one of them has an extra character at the end. equalsIgnoreCase
can return immediately; compareToIgnoreCase
has to iterate to the end of the string to see the difference.
equalsIgnoreCase可以快得多。例如,考虑两个以相同的10,000个字符开头的字符串 - 但其中一个字符串最后有一个额外的字符。 equalsIgnoreCase可以立即返回; compareToIgnoreCase必须迭代到字符串的末尾才能看到差异。
But generally I'd go with whichever expresses your intention better. This works well for performance too: assuming I'm right in saying that equalsIgnoreCase
is at least as fast as compareToIgnoreCase
, it means you should use that where you can - if you need an actual ordering, you've got to use compareToIgnoreCase
anyway.
但一般来说,我会选择更好地表达你的意图。这也适用于性能:假设我说equalsIgnoreCase至少和compareToIgnoreCase一样快,这意味着你应该尽可能地使用它 - 如果你需要实际的排序,你必须使用compareToIgnoreCase。
#2
if you worry about performances... measure it
如果你担心表演......测量它
#3
Looking at the source for java.lang.String
查看java.lang.String的源代码
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true :
(anotherString != null) && (anotherString.count == count) &&
regionMatches(true, 0, anotherString, 0, count);
}
So, before it looks at the actual string character by character (which also happens in a similar fashion for compareToIgnoreCase
), equalsIgnoreCase
also checks for reference identity and character length, which could be very much faster.
因此,在它逐个字符地查看实际的字符串之前(对于compareToIgnoreCase也会以类似的方式发生),equalsIgnoreCase还会检查引用标识和字符长度,这可能会非常快。
#1
equalsIgnoreCase
can be a lot faster. For example, consider two strings which start with the same 10,000 characters - but one of them has an extra character at the end. equalsIgnoreCase
can return immediately; compareToIgnoreCase
has to iterate to the end of the string to see the difference.
equalsIgnoreCase可以快得多。例如,考虑两个以相同的10,000个字符开头的字符串 - 但其中一个字符串最后有一个额外的字符。 equalsIgnoreCase可以立即返回; compareToIgnoreCase必须迭代到字符串的末尾才能看到差异。
But generally I'd go with whichever expresses your intention better. This works well for performance too: assuming I'm right in saying that equalsIgnoreCase
is at least as fast as compareToIgnoreCase
, it means you should use that where you can - if you need an actual ordering, you've got to use compareToIgnoreCase
anyway.
但一般来说,我会选择更好地表达你的意图。这也适用于性能:假设我说equalsIgnoreCase至少和compareToIgnoreCase一样快,这意味着你应该尽可能地使用它 - 如果你需要实际的排序,你必须使用compareToIgnoreCase。
#2
if you worry about performances... measure it
如果你担心表演......测量它
#3
Looking at the source for java.lang.String
查看java.lang.String的源代码
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true :
(anotherString != null) && (anotherString.count == count) &&
regionMatches(true, 0, anotherString, 0, count);
}
So, before it looks at the actual string character by character (which also happens in a similar fashion for compareToIgnoreCase
), equalsIgnoreCase
also checks for reference identity and character length, which could be very much faster.
因此,在它逐个字符地查看实际的字符串之前(对于compareToIgnoreCase也会以类似的方式发生),equalsIgnoreCase还会检查引用标识和字符长度,这可能会非常快。