Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Machine > Re: Optimizatio...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 3 Topic 790 of 843
Post > Topic >>

Re: Optimization question

by glen herrmannsfeldt <gah@[EMAIL PROTECTED] > Sep 27, 2007 at 06:06 PM

Christopher Diggins wrote:

> Quick question about optimizing JVM bytecode. Suppose I have the
> following Java code:

> public static void main(String[] args) {
>   int a[] = new int[42];
>   for (int i=0; i < a.length; ++i)
>     a[i] = 1;
>   int sum = 0;
>   for (int i=0; i < a.length; ++i)
>      sum = sum + a[i] ;
>    System.out.println(sum);
> }

> Would it be okay if I wrote an optimizer that pre-evaluated the code
> and just generated byte-code that output the value 42? 

This is very common for optimizers for other languages.  My 
understanding for Java is that the exception model limits some
optimizations that might otherwise be possible.   In this case
you can easily show that no exceptions will occur so it should
be legal.

The only one I can see that could possibly happen is that
yours would still work even if not enough memory was available.
Consider the slightly different:

public static void main(String[] args) {
   int a[] = new int[1000000000];
   for (int i=0; i < a.length; ++i)
     a[i] = 1;
   int sum = 0;
   for (int i=0; i < a.length; ++i)
      sum = sum + a[i] ;
    System.out.println(sum);
}

This would fail on most systems without optimization, but with
yours it would not fail.   In more complicated programs there is
interaction with other uses of memory.

-- glen
 




 3 Posts in Topic:
Optimization question
Christopher Diggins <c  2007-09-22 14:55:05 
Re: Optimization question
Joshua Cranmer <Pidgeo  2007-09-22 23:01:53 
Re: Optimization question
glen herrmannsfeldt <g  2007-09-27 18:06:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 14:42:05 CST 2008.