Lists Home |
Date Index |
- From: Tyler Baker <firstname.lastname@example.org>
- To: David Megginson <email@example.com>
- Date: Mon, 29 Mar 1999 10:26:09 -0500
David Megginson wrote:
> Bill la Forge writes:
> > It would be great if filters had the same advantages as parsers in
> > being able to simply test for equality (x==y) rather than having to
> > do a string comparison (x.equals(y)) when checking for a specific
> > element or attribute name.
> Yes, but as someone (James Clark?) pointed out during the last round,
> with most serious applications you're going to end up doing hash
> lookups anyway, so the == doesn't buy you much.
That depends on your implementation of a hash table. Also as of JDK 1.1.6 the equals method
for strings first tests for identity of the two string objects and then tests to see if the
length is the same and then tests for matching of each character in each string. When dealing
with names in XML they are uniformly nothing more than symbols so in application code being
able to do something like this:
if (x == "foo")
is generally much faster than:
as you do not incur the overhead of calling one dynamic method. Really it depends on your
code. In an XML related technology I worked on I had lots of if-else statements that did
exactly this. The parser I used presented the strings to the application as interned strings
and did significantly improve performance from using the equals method approach.
Another thing that I used for speeding up my applications is to have a special hash table for
interned strings. Basically all that this table did was use System.identityHashcode() instead
of String.hashcode() to get a hash for the string. In effect you use the Object.hashCode()
It also depends a lot on your VM. Some VM's are good enough with dynamic method invocation
that the difference between testing for string identity and string equality is neglibible.
The so-called Hotspot VM may even inline String.equals() into your code.
I suggest using the identity approach if possible as it is easier to read and maintain IMHO
and in the general case you may get significant speedups if your application does many string
comparisons. If you need a faster hash table for strings build one yourself.
xml-dev: A list for W3C XML Developers. To post, mailto:firstname.lastname@example.org
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:email@example.com the following message;
To subscribe to the digests, mailto:firstname.lastname@example.org the following message;
List coordinator, Henry Rzepa (mailto:email@example.com)