Code efficiency - declare variable sinside or outdie a loop.

Discussion in 'Java' started by Ed Thompson, Aug 1, 2004.

  1. Ed Thompson

    Ed Thompson Guest

    I have long had the question of whether is is better to declare a
    vraible inside a loop or outside a loop in Java.

    To test I wrote the following code:

    public class LoopTest {
    public void inside() {
    for(int i=0; i< 10;i++)
    {
    String x = "Edward";
    }
    }
    public void outside() {
    String x = null;
    for(int i=0; i< 10;i++)
    {
    x = "Edward";
    }
    }
    }

    Which I disassembled using javap -c:

    public void inside();
    Code:
    0: iconst_0
    1: istore_1
    2: iload_1
    3: bipush 10
    5: if_icmpge 17
    8: ldc #2; //String Edward
    10: astore_2
    11: iinc 1, 1
    14: goto 2
    17: return

    public void outside();
    Code:
    0: aconst_null
    1: astore_1
    2: iconst_0
    3: istore_2
    4: iload_2
    5: bipush 10
    7: if_icmpge 19
    10: ldc #2; //String Edward
    12: astore_1
    13: iinc 2, 1
    16: goto 4
    19: return

    If I read this right, it indicates that 'redelcaring' the variable
    inside the loop is 2 opcodes shorter The first two opcodes in outside()
    are not in inside()).

    So now I have an interpretation issue. Does this really mean that
    inside() is faster than outside()? Is one really more optimal?

    Can someone help explain what is happening? (Not the line by line, but
    what is java really doing?)

    Thanx
    Ed Thompson, Aug 1, 2004
    #1
    1. Advertising

  2. Ed Thompson

    David Hilsee Guest

    "Ed Thompson" <> wrote in message
    news:nxVOc.162472$...
    > I have long had the question of whether is is better to declare a
    > vraible inside a loop or outside a loop in Java.
    >
    > To test I wrote the following code:
    >
    > public class LoopTest {
    > public void inside() {
    > for(int i=0; i< 10;i++)
    > {
    > String x = "Edward";
    > }
    > }
    > public void outside() {
    > String x = null;
    > for(int i=0; i< 10;i++)
    > {
    > x = "Edward";
    > }
    > }
    > }
    >
    > Which I disassembled using javap -c:
    >
    > public void inside();
    > Code:
    > 0: iconst_0
    > 1: istore_1
    > 2: iload_1
    > 3: bipush 10
    > 5: if_icmpge 17
    > 8: ldc #2; //String Edward
    > 10: astore_2
    > 11: iinc 1, 1
    > 14: goto 2
    > 17: return
    >
    > public void outside();
    > Code:
    > 0: aconst_null
    > 1: astore_1
    > 2: iconst_0
    > 3: istore_2
    > 4: iload_2
    > 5: bipush 10
    > 7: if_icmpge 19
    > 10: ldc #2; //String Edward
    > 12: astore_1
    > 13: iinc 2, 1
    > 16: goto 4
    > 19: return
    >
    > If I read this right, it indicates that 'redelcaring' the variable
    > inside the loop is 2 opcodes shorter The first two opcodes in outside()
    > are not in inside()).
    >
    > So now I have an interpretation issue. Does this really mean that
    > inside() is faster than outside()? Is one really more optimal?
    >
    > Can someone help explain what is happening? (Not the line by line, but
    > what is java really doing?)


    Why not try measuring the performance at runtime instead of examining
    opcodes? While it may the opcodes may be interesting, executing code will
    probably provide a more straightforward answer about its efficiency.

    --
    David Hilsee
    David Hilsee, Aug 1, 2004
    #2
    1. Advertising

  3. Ed Thompson

    Dave Glasser Guest

    Ed Thompson <> wrote on Sat, 31 Jul 2004
    23:01:39 GMT in comp.lang.java.programmer:

    >So now I have an interpretation issue. Does this really mean that
    >inside() is faster than outside()? Is one really more optimal?


    Whatever difference in speed there might be would be so infinitesimal
    that it wouldn't be worth worrying about, IMO. I would always declare
    the variable inside the loop since it's less bug prone than outside,
    particularly if you're working with nested loops.

    Besides, another compiler might optimize the code in such a way that
    the the actual bytecode is identical for the two methods.


    --
    Check out QueryForm, a free, open source, Java/Swing-based
    front end for relational databases.

    http://qform.sourceforge.net
    Dave Glasser, Aug 1, 2004
    #3
  4. Ed Thompson wrote:
    > I have long had the question of whether is is better to declare a
    > vraible inside a loop or outside a loop in Java.
    >
    > To test I wrote the following code:
    >
    > public class LoopTest {
    > public void inside() {
    > for(int i=0; i< 10;i++)
    > {
    > String x = "Edward";
    > }
    > }
    > public void outside() {
    > String x = null;
    > for(int i=0; i< 10;i++)
    > {
    > x = "Edward";
    > }
    > }
    > }
    >
    > Which I disassembled using javap -c:
    >
    > public void inside();
    > Code:
    > 0: iconst_0
    > 1: istore_1
    > 2: iload_1
    > 3: bipush 10
    > 5: if_icmpge 17
    > 8: ldc #2; //String Edward
    > 10: astore_2
    > 11: iinc 1, 1
    > 14: goto 2
    > 17: return
    >
    > public void outside();
    > Code:
    > 0: aconst_null
    > 1: astore_1
    > 2: iconst_0
    > 3: istore_2
    > 4: iload_2
    > 5: bipush 10
    > 7: if_icmpge 19
    > 10: ldc #2; //String Edward
    > 12: astore_1
    > 13: iinc 2, 1
    > 16: goto 4
    > 19: return
    >
    > If I read this right, it indicates that 'redelcaring' the variable
    > inside the loop is 2 opcodes shorter The first two opcodes in outside()
    > are not in inside()).
    >
    > So now I have an interpretation issue. Does this really mean that
    > inside() is faster than outside()? Is one really more optimal?
    >
    > Can someone help explain what is happening? (Not the line by line, but
    > what is java really doing?)


    The only difference is aconst_null, astore_1 in the second version,
    which can be removed by an optimizer anyway (in fact, the whole code can
    be compressed to a single return.) Always declare variables in as
    limited a scope as possible, unless you have a specific reason not to do
    so.
    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Aug 1, 2004
    #4
  5. Ed Thompson

    Liz Guest

    "Ed Thompson" <> wrote in message
    news:nxVOc.162472$...
    > I have long had the question of whether is is better to declare a
    > vraible inside a loop or outside a loop in Java.
    >
    > To test I wrote the following code:
    >
    > public class LoopTest {
    > public void inside() {
    > for(int i=0; i< 10;i++)
    > {
    > String x = "Edward";
    > }
    > }
    > public void outside() {
    > String x = null;
    > for(int i=0; i< 10;i++)
    > {
    > x = "Edward";
    > }
    > }
    > }
    >
    > Which I disassembled using javap -c:
    >
    > public void inside();
    > Code:
    > 0: iconst_0
    > 1: istore_1
    > 2: iload_1
    > 3: bipush 10
    > 5: if_icmpge 17
    > 8: ldc #2; //String Edward
    > 10: astore_2
    > 11: iinc 1, 1
    > 14: goto 2
    > 17: return
    >
    > public void outside();
    > Code:
    > 0: aconst_null
    > 1: astore_1
    > 2: iconst_0
    > 3: istore_2
    > 4: iload_2
    > 5: bipush 10
    > 7: if_icmpge 19
    > 10: ldc #2; //String Edward
    > 12: astore_1
    > 13: iinc 2, 1
    > 16: goto 4
    > 19: return
    >
    > If I read this right, it indicates that 'redelcaring' the variable
    > inside the loop is 2 opcodes shorter The first two opcodes in outside()
    > are not in inside()).
    >
    > So now I have an interpretation issue. Does this really mean that
    > inside() is faster than outside()? Is one really more optimal?
    >
    > Can someone help explain what is happening? (Not the line by line, but
    > what is java really doing?)
    >
    > Thanx
    >
    >
    >

    You should measure, but my book on performance says that the first
    4 variables inside a method (this includes the parameters) are faster
    to access than the rest. And that local variables are faster
    than fields.
    Liz, Aug 1, 2004
    #5
  6. Ed Thompson

    Chris Uppal Guest

    Ed Thompson wrote:

    > public void outside() {
    > String x = null;
    > for(int i=0; i< 10;i++)
    > [...]


    > public void outside();
    > Code:
    > 0: aconst_null
    > 1: astore_1


    The two methods result in /almost/ the same opcodes, the difference is that the
    first two of outside() initialise the 'x' variable to null explicitly -- which
    is just what your code in outside() asked for. Your code for inside() did not
    ask for that extra step, and hence requires fewer instructions.

    That extra operation could, I suppose, in sufficiently bizarre circumstances
    make a measurable difference. Normally it won't make any difference at all
    because:

    a) it is tiny

    b) it is /outside/ the innermost loop, and the innermost loop is normally where
    most of the time is spent.

    -- chris
    Chris Uppal, Aug 1, 2004
    #6
  7. Ed Thompson

    Ed Thompson Guest

    >> Always declare variables in as limited a scope as possible, unless
    you have a specific reason not to do
    > so.


    MY concern was that inside the loop, a new Reference variable to
    "Edward" would be created each time, while outside the loop the same
    Refenece variable would be reused.

    Was trying to avoid allocate/deallocate loop
    Ed Thompson, Aug 1, 2004
    #7
  8. Ed Thompson

    Roedy Green Guest

    On Sat, 31 Jul 2004 23:01:39 GMT, Ed Thompson
    <> wrote or quoted :

    >I have long had the question of whether is is better to declare a
    >vraible inside a loop or outside a loop in Java.


    Declare a variable so that it has minimal scope to do the job. So, if
    you can declare it inside the loop, this is better.

    All local variables are allocated when you first enter a method, all
    of them at once by incrementing the stack pointer, not where they
    logically appear to be allocated, so there is no overhead for
    "allocating" them over and over inside a loop.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Aug 1, 2004
    #8
  9. Ed Thompson

    xarax Guest

    "Ed Thompson" <> wrote in message
    news:w16Pc.226798$...
    > >> Always declare variables in as limited a scope as possible, unless

    > you have a specific reason not to do
    > > so.

    >
    > MY concern was that inside the loop, a new Reference variable to
    > "Edward" would be created each time, while outside the loop the same
    > Refenece variable would be reused.
    >
    > Was trying to avoid allocate/deallocate loop


    Totally wrong thinking.

    The compiler will allocate stack frame slots according
    to the entire method. When the method is called, the
    entire stack frame is allocated at that time. While
    the method is running, slots in the stack frame are
    used according to the compiler's allocation.

    Think of the stack frame as a machine array that
    is indexed by integers. The variable "x" inside
    the loop may have been assigned by the compiler
    to stack frame slot 4. The slot already exists
    when the method is entered. The code just stuffs
    the datum into slot 4. stackframe[4] = <datum>.

    It won't matter at all whether the variable is
    declared inside or outside of the loop. Note that
    you *are* assigning null to the outside variable,
    so that code must be generated, even though stack
    frame slots are initialized to null or zero.
    xarax, Aug 1, 2004
    #9
  10. Ed Thompson

    Ed Thompson Guest

    Just the kind of perspective I was looking for, thanx!
    Ed Thompson, Aug 1, 2004
    #10
  11. Ed Thompson

    Ed Thompson Guest

    Roedy Green wrote:

    > All local variables are allocated when you first enter a method, all
    > of them at once by incrementing the stack pointer, not where they
    > logically appear to be allocated, so there is no overhead for
    > "allocating" them over and over inside a loop.
    >

    Thanx, I was looking for this kind of info!
    Ed Thompson, Aug 1, 2004
    #11
  12. Ed Thompson

    Roedy Green Guest

    On Sun, 01 Aug 2004 17:59:58 GMT, Ed Thompson
    <> wrote or quoted :

    >Thanx, I was looking for this kind of info!


    Use javap to disassemble some of your code. You can then see what sort
    of JVM byte codes it generates. If you read the JVM spec, all this
    will come clear pretty quickly. If you are new to Java, but are an
    experienced assembler programmer, I'd also recommend this approach to
    help undersand oo by leveraging your low-level understanding.

    JVM is a very easy assembler to learn.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Aug 1, 2004
    #12
  13. Ed Thompson

    Chris Smith Guest

    Liz wrote:
    > You should measure, but my book on performance says that the first
    > 4 variables inside a method (this includes the parameters) are faster
    > to access than the rest.


    Again, this says more about the suspect character of the book than about
    performance. There are bytecodes designated to make code that
    references the first four variables a lot shorter, but that doesn't mean
    faster. What's faster will depend on the local processor architecture,
    where there's not likely to be any special optimizations for any local
    variable access.

    > And that local variables are faster than fields.


    That one probably is true, because of cache locality issues. The local
    stack is nearly always cached, so accesses take practically no time.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Aug 2, 2004
    #13
  14. Ed Thompson

    Liz Guest

    "Chris Smith" <> wrote in message
    news:...
    > Liz wrote:
    > > You should measure, but my book on performance says that the first
    > > 4 variables inside a method (this includes the parameters) are faster
    > > to access than the rest.

    >
    > Again, this says more about the suspect character of the book than about
    > performance. There are bytecodes designated to make code that
    > references the first four variables a lot shorter, but that doesn't mean
    > faster. What's faster will depend on the local processor architecture,
    > where there's not likely to be any special optimizations for any local
    > variable access.
    >
    > > And that local variables are faster than fields.

    >
    > That one probably is true, because of cache locality issues. The local
    > stack is nearly always cached, so accesses take practically no time.
    >
    > --
    > www.designacourse.com
    > The Easiest Way to Train Anyone... Anywhere.
    >
    > Chris Smith - Lead Software Developer/Technical Trainer
    > MindIQ Corporation
    Liz, Aug 3, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vamshi

    Efficiency of the for loop

    vamshi, Oct 16, 2006, in forum: C Programming
    Replies:
    19
    Views:
    562
    Richard
    Oct 16, 2006
  2. Martin

    Loop Efficiency

    Martin, Feb 23, 2009, in forum: Java
    Replies:
    17
    Views:
    682
    Tom Anderson
    Feb 27, 2009
  3. Ken Fine
    Replies:
    4
    Views:
    125
    Steve Kushubar
    Sep 18, 2003
  4. tuser
    Replies:
    2
    Views:
    224
    DJ Stunks
    Jun 12, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page