compiler optimization

From: Murat Tasan (tasan_at_eecs.cwru.edu)
Date: 02/23/04

  • Next message: Nate Smith: "Re: src.zip"
    Date: Sun, 22 Feb 2004 21:39:19 -0500
    
    

    i've asked a similar question to this in the past, but the example i gave
    was not good enough i think to warrant any good answers... so i've come up
    with another example that should be clear.

    consider a loop that adds some values from an array of doubles x... such
    as (where "a" is a constant value):

    for(int i = 0; i < x.length; i++)
            result += (x[i] - a) / x.length;

    now, one can also obviously do:

    for(int i = 0; i < x.length; i++)
            result += (x[i] - a);
    result = result / x.length;

    if the code is executed directly as it is written, the second version can
    be far more efficient (only one division, vs x.length divisions)... and
    let us assume that precision is not a direct concern at the moment.

    does anyone know if the java compiler will attempt to see this
    optimization and make the appropriate change if the code is given as it is
    in the first example?

    thanks much,

    murat

    -- 
    Murat Tasan
    mxt6@po.cwru.edu
    tasan@eecs.cwru.edu
    murat.tasan@cwru.edu
    http://genomics.cwru.edu
    

  • Next message: Nate Smith: "Re: src.zip"