Initialising

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    #1

    Initialising

    I have some variables which I just need for a certain loop, so I would like to ask is it better to initialise them inside a loop or before you enter a loop. I would like my algorithm to run faster.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Initializing them inside the loop means the initialization is done as many times as the loop is run.

    Comment

    • thatos
      New Member
      • Aug 2007
      • 105

      #3
      Originally posted by r035198x
      Initializing them inside the loop means the initialization is done as many times as the loop is run.
      So it will be better to initialize them outside a loop?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        How about some more info?
        What kind of loop are you using? for, while e.t.c
        Are the initial values of the variables known before the loop is run?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Also javap, the disassembler can give valuable information about the generated
          code; it is part of Sun's jdk.

          kind regards,

          Jos

          Comment

          Working...