Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Java optimization.

Thread view: 
Serguei.Goumeniouk@cibc.ca - 10 Dec 2007 21:14 GMT
Dear Experts,
I have the following lines in my codes to test a string:

String str = " ... some integer ...";
try {
  int value = Integer.parseInt(str);
} catch (Exception ex) {
  // This is not an integer
}

The variable "value" is not used anymore, so a clever compiler may
remove the try block.
In my Java environment the compiler does not "optimize" these lines
and everything works OK.
What I am worrying about is the following: "Are there any known Java
compilers which will optimize these codes?"

  Regards,
    Serguei.
Mark Thornton - 10 Dec 2007 21:45 GMT
>    Dear Experts,
> I have the following lines in my codes to test a string:
[quoted text clipped - 15 lines]
>    Regards,
>      Serguei.

Provided you have something more than a comment in the catch clause then
no legal implementation of Java could optimise the lines away.

Mark Thornton
Joshua Cranmer - 10 Dec 2007 21:47 GMT
>    Dear Experts,
> I have the following lines in my codes to test a string:
[quoted text clipped - 8 lines]
> The variable "value" is not used anymore, so a clever compiler may
> remove the try block.

But even though value may not be used, the action of Integer.parseInt
may have some side effects. Suppose str is set to be "dw23f~~"? The
value of str would have to be known (or limited in some form) and the
semantics of Integer.parseInt would have to be guaranteed to not throw
an exception.

One can argue that the call related to the assignment of the value
should be optimized as well, but some assignments for unused variables
may be purposeful in effect.

I can't see how the optimization would avoid violating the JLS in these
circumstances without requiring serious in-depth analysis of the code
and also keeping binary compatibility.

As a final note: a try-catch statement adds very little overhead when
not called, and the assignment is equally negligible in effect.

> In my Java environment the compiler does not "optimize" these lines
> and everything works OK.

The Sun Java compiler does not optimize because optimized code hampers
the ability of the JIT optimizer to optimize the code (although variable
propagation analysis probably doesn't have an impact).

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.