For Loop or While Loop - Efficiency

前端 未结 8 1882
遥遥无期
遥遥无期 2021-01-12 08:03

This may be a stupid question, but how does the efficiency of a while loop compare to that of a for loop? I\'ve always been taught that if you can use a for loop,

相关标签:
8条回答
  • 2021-01-12 08:10

    That depends on the exact compiler that you use. In your example, a good compiler will create the same machine code for both options.

    0 讨论(0)
  • 2021-01-12 08:16

    It will depend slightly on the language, possibly, and maybe on the compiler, but most modern compilers will treat them as exactly the same and there will be no difference.

    0 讨论(0)
  • 2021-01-12 08:23

    The performance difference between for loop and while loop is not a big issue because modern compilers can generate same machine code for both loops and secondly both loops require same operations:

    1. Initialization of counter variable.
    2. Test condition.
    3. Increment / decrement in counter variable.

    In general you should use for loop in your code for the following reasons:

    1. To increase the readability of your code.
    2. To improve the maintainability of your code since all three major parts of a loop i.e. initialization, increment / decrement and test condition are written at the same line(in most cases).
    3. It limits the scope of counter variables better than a while loop, hence it helps in better memory management.

    I hope that makes sense and would help.

    0 讨论(0)
  • 2021-01-12 08:23

    Well, if the loop is large and complexe it won't matter since the overhead of the loop code (for or while) will be very low...

    Anyway, if you really want to know, I guess it's up to you to check in your ide, in assembly code. or you can use a dissembler to look yourself in an executable. http://www.caesum.com/files/borg228.zip (warning: there is usually lots of bloat in .exe,so good luck!)

    0 讨论(0)
  • 2021-01-12 08:26

    As mentioned in all the answers here, any decent compiler would compile both loops into the same machine code.

    Your machine code (taking MIPS as an ex) would be a bunch of normal assembly statements followed by a branch (onequal/notequal) in both cases making your efficiency consistent.

    However, you could debate over the coding style issue over here (Not efficiency).

    For Loops :

    1. Used when you know EXACTLY how many times the loop is going to run. Exit case is known.
    2. Know by the amount by which your loop is going to increment by on each iteration

    *Probable Usage : When a collection of items already exists and you want to go over it and retrieve the number of times a certain property appears.

    While Loops :

    1. You're not aware of how many times the loop is going to run. There is an exit case which is set/reached sometime while the loop is running (if you want to simulate a for loop, you'll use something like a counter (extra code))

    2. Do not know by how much your loop is going to increment by. Your increment/next move can be set dynamically. While you can do this in a for loop, you'll have to account for the increment at each iteration as well causing some unreadable code which can be avoided when if you use a while loop.

    *Probable Usage : Grepping a stream for some data. You're not aware of how long the stream is so your exit case is when the stream ends. Since it's a stream and you might be getting data line by line, you might want to skip over white lines altogether making your increments not consistent.

    0 讨论(0)
  • 2021-01-12 08:28

    I think you're drawing the wrong conclusion from the advice you've been given.

    The reason (in this instance at least) to prefer the for construct over the while has nothing to do with efficiency; it's all about writing code that expresses your intentions in a clear and easy to understand manner.

    The for places the initial condition, increment, and exit condition all in one place, making it easier to understand. The while loop spreads them around. For example, in your sample, what is the initial value of i? -oh, you forgot to specify it? --that's the point.

    0 讨论(0)
提交回复
热议问题