[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: "some text".equals(someString)



>  --- "E.L. Willighagen" <egonw@sci.kun.nl> wrote:
> >
> > Hi all,
> >
> > can anyone tell me the difference (performance etc) between:
> >
> > 1. String someString = "text"
> >    if ("some text".equals(someString)) {};
> >
> > and
> >
> > 2. String someString = "text"
> >    if (someString.equals("some text")) {};
> >


There are at least two levels of abstraction which will affect the 
performance -- 1. the nature of the bytecode generated by the java 
compiler, and 2. the interpretation of the bytecode by the JVM.

Relative to 2., different JVMs will indeed have remarkably different
performance for the same bytecode file.  Since this is always true,
your question appears to be related more to 1 (i.e., bytecode
differences 
for the same JVM.)

Relative to 1., try disassembling the bytecode files for each
alternative 
with javap and see if there are any differences in the bytecode  If
there 
are, you could look up the instructions in question in the "Java Virtual 
Machine Specification" and see which case uses the more complex
instructions.
This case will probably, but not necessarily, run slower.  This approach
will give some JVM-independent insight.

Alternatively, try running a timing test of a small program written each
way.
You will find a com.jreality.util.StopWatch class on my site at
www.jreality.com/downloads/ in the Measure Units Conversin Package jar
file
which may be helpful for this.  For a code snippet this samll, set the 
iteration count to a million or so iterations to get reliable times to
compare.
The results, of course, will only be valid for the JVM and host on which
you
run the test.

Hope this helps.

Rick
-- 
                  Rick
                  Lutowski
|________rick@jreality.com__ 
 \ oo                       \____http://www.jreality.com/_________ 
__\ ____________________________________________________________ /_
   |                                                       _____/
   `------------------------------------------------------'



Reply to: