Jeremias Maerki wrote:
I don't think the discussion was finished when you did this, Glen.
[Glen Mazza]
Oh no, it wasn't--but we know the status quo *wasn't* acceptable, and
that the performance argument was no longer valid because of the
research you did on .equals().
Not so fast. Measurements are always better than guesses. And there *is*
a difference in performance.
We should not intern strings, unless we have very special reasons,
better reasons than performance. Unless SAX promises that the URI
strings are interned, then we can't depend on receiving interned strings.
But since == *is* faster then .equals and I think we can assume that
most URIs are in fact from the FO namespace we can get the benefit from
both.
Measured with jdk1.4.2_02 on winXP:
Equal string
== 141
.equals 1938
== || .equals 203
Different string
== 140
.equals 1844
== || .equals 1891
regards,
finn
public class c1 {
public static void main(String[] args) {
String s1 = "http://xml.org/sax/features/string-interning";
String s2 = "http://xml.org/sax/features/string-interning";
String s3 = "http://xml.org/sax/features/namespace";
long before;
long after;
System.out.println("Equal string");
cmp(s1, s2);
cmp(s1, s2);
before = System.currentTimeMillis();
cmp(s1, s2);
after = System.currentTimeMillis();
System.out.println("== " + (after - before));
equals(s1, s2);
equals(s1, s2);
before = System.currentTimeMillis();
equals(s1, s2);
after = System.currentTimeMillis();
System.out.println(".equals " + (after - before));
cmpequals(s1, s2);
cmpequals(s1, s2);
before = System.currentTimeMillis();
cmpequals(s1, s2);
after = System.currentTimeMillis();
System.out.println("== || .equals " + (after - before));
System.out.println("Different string");
cmp(s1, s3);
cmp(s1, s3);
before = System.currentTimeMillis();
cmp(s1, s3);
after = System.currentTimeMillis();
System.out.println("== " + (after - before));
equals(s1, s3);
equals(s1, s3);
before = System.currentTimeMillis();
equals(s1, s3);
after = System.currentTimeMillis();
System.out.println(".equals " + (after - before));
cmpequals(s1, s3);
cmpequals(s1, s3);
before = System.currentTimeMillis();
cmpequals(s1, s3);
after = System.currentTimeMillis();
System.out.println("== || .equals " + (after - before));
}
public static void cmp(String s1, String s2) {
for (int i = 0; i < 100000000; i++) {
if (s1 == s2) {
continue;
}
}
}
public static void equals(String s1, String s2) {
for (int i = 0; i < 100000000; i++) {
if (s1.equals(s2)) {
continue;
}
}
}
public static void cmpequals(String s1, String s2) {
for (int i = 0; i < 100000000; i++) {
if (s1 == s2 || s1.equals(s2)) {
continue;
}
}
}
}