As I think, hashCode () can more quickly compare two string comparisons.
Arguments?
Arguments against this proposal:
hashcode() from String should access each character in the string and perform 2 calculations for each character.
Therefore, we need a string with n characters of 5*n operations (load, multiply, search / load, multiply, save). Two times because we are comparing two lines. (Well, one repository and one load are not really taken into account in a reasonable implementation.)
In the best case, this does the 10*x operation for two lines with lengths m and n and x=min(m,n) . The worst case is 10*x with x=m=n . The average is somewhere between, possibly (m*n)/2 .
Current is equal to implementation needs in the best cases 3 . 2 loads, 1 compare. The worst thing is 3*x operations for two lines with lengths m and n and x=m=n . The average is somewhere between maybe 3*(m*n)/2 .
- Even if we cache the hash, it’s not clear that we are saving anything
We need to analyze usage patterns. It may be that most of the time we will ask once for equal, and not several times. Even if we ask a few times, this is not enough to save time on caching.
Not directly against speed, but still good counterarguments:
We do not expect hashcode to equal equals, because we know for sure that hash(a)==hash(b) for some a!=b Everyone who reads this (and knowledge of hashing) is wondering what is going on there.
- leads to unsuccessful examples / unexpected behavior
I already see the following question about SO: “I have a line with a billion times“ a. ”Why is it worth forever comparing it with equal () versus“ b ”? :)
tb - Jan 10 '13 at 18:01 2013-01-10 18:01
source share