Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Optimisation



Stephen Paulsen wrote:

>By the way, a switch() isn't equivalent to writing if/then/else if/then
>in any case. Using if/then generates a cascade of 'if_icmpne' bytecode
>instructions, while switch generates a single 'lookupswitch' instruction
>and a jump table.

'switch' can also generate a tableswitch, in which 'switch' values are
direct indices into a jump table.  The compiler will choose tableswitch
instead of lookupswitch if the case values and the case code are dense
enough.

The 'javap -c ...' command will show what bytecodes are compiled in any
method.  It's worth looking if you need to know.

The lookupswitch does have to search its table of value/target pairs using
comparison, but it can be faster than a search embodied by a sequence of
if/elseif's.  However, one can't really tell which is faster except by
profiling.  For example, an if/else chain whose tested values are arranged
in decreasing frequency might be faster than tableswitch or lookupswitch,
simply because the most frequently appearing values are recognized soonest.
There's a point of diminishing returns on that, though.  So a few if/else's
followed by a switch may be best.  Or not.

You always have to measure the result.  Without measurements, there's no
evidence for ANY optimization tactic.  Optimization without evidence is
merely hoping.  You might well be pessimizing the code despite your best
hopes and intentions.


This monthly has lots of links, and 4 yrs of back-issues:
  <http://www.javaperformancetuning.com/>

  -- GG


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.