String interning (WAS: SAX2/Java: Towards a final form)

David Brownell david-b at pacbell.net
Fri Jan 14 20:09:59 GMT 2000


[ I got two copies of this, one direct and one to list -- I
responded to the former, just noticed the latter, am summarizing
a key point here: ]

This is highly dependant on "N", and the type of work being
done in those branches.  I've typically observed N to be low,
on the order of a dozen (usually less).  Averaging out to N/2
comparisons, at two instructions each (test, branch), that's
easily less than the cost of two method calls alone (lookup,
dispatch to handler), not to omit the lookup itself (e.g. a
hash, bucket selection, bucket search, lock acquisition and
release, etc).  

The suggested pattern is useful in some cases, but not all.
I use the "==" chain a hundred times before I come across a
case where the dispatch object is appropriate; and that's
as true in applications as in infrastructure.

- Dave


Miles Sabin wrote:
> 
> 	 the only time where the performance difference
> between,
> 
>   foo == "literal"
> 
> and,
> 
>   foo.equals("literal")
> 
> is going to be significant is where there are long chains
> of comparisons, ie.,
> 
>   if(foo == "elem1")
>     // do stuff for elem1
>   else if(foo == "elem2")
>     // do stuff for elem2
> 
>   // ... repeat many times ...
> 
>   else if (foo == "elemn")
>     // do stuff for elemn
> 
> where it'd be a far better idea (both from performance and
> software design POVs) to do something like,
> 
>   ElementHandler handler =
>     (ElementHandler)someTable.lookup(foo);
> 
>   handler.doStuffForElement();
> 
> Cheers,
> 
> Miles
> 
> --
> Miles Sabin                       Cromwell Media

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ or CD-ROM/ISBN 981-02-3594-1
Please note: New list subscriptions now closed in preparation for transfer to OASIS.





More information about the Xml-dev mailing list