Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6781499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:37:52+00:00 2026-05-26T16:37:52+00:00

I have profiled for , while and do-while loops with something simple: while ($var

  • 0

I have profiled for, while and do-while loops with something simple:

while ($var < 1000000) {
  ++$var;
}

do {
  ++$var;
} while ($var < 1000000);

for ($var = 0; $var < 1000000; ++$var) {
  //do nothing
}

by comparing microtime() before and after the loops.

The do-while loop is by a considerable amount the fastest loop. do-while is actually faster than while by almost half. I know that they are for different purposes ( while checks the condition before the loop executes and do-while executes at least once ).

I know the general consensus is that while loops are frowned upon and do-while even more so.

My question is why? Considering how many for loops are used in PHP applications, shouldn’t do-while be used more? Even with an if statement to check a condition before the loop executes, the performance boost is considerable.

My currently accepted answer is that code legibility is the suspect.

10 year Edit:
10 years ago, I was asked this question at a job interview. I went into the interview with the incorrect perception that while loops were frowned upon. I learned this from my previous job where while loops were not allowed in code, as per instruction from my superior.

The interview had gone well with management, then I was handed over to the lead programmer, he asked me what the fastest loop in PHP was, which I got incorrect and I did not get the job, this is why I asked the question on SO.

10 years of experience has taught me a lot.

  1. while loops are fine (its ludicrous to think I was taught otherwise)
  2. Micro-optimizations are truly evil (profile code, focus on bottle necks)
  3. in 10 years, I have never re-written a loop for speed. I have however rewritten the logic inside the loop which was always the true bottleneck.
  4. There is a lot of strongly held but largely incorrect opinions in the programming field. Stay the course, read up, experiment and question, be open to learning from better programmers and dont be afraid to be wrong.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:37:53+00:00Added an answer on May 26, 2026 at 4:37 pm
    1. Micro optimizations are evil. They reduce readability for no measurable performance gain. Even if your application does have loops with millions of iterators (which I doubt) the difference is still negligible.
    2. The difference between while / do while is smaller than you say: http://codepad.viper-7.com/M8cgt9
    3. To understand why do while is marginally faster, look at the generated opcodes:

      line     # *  op                           fetch          ext  return  operands
      ---------------------------------------------------------------------------------
      # while loop
         3     0  >   ASSIGN                                                   !0, 0
         4     1  >   IS_SMALLER                                       ~1      !0, 1000000
               2    > JMPZ                                                     ~1, ->5
               3  >   PRE_INC                                                  !0
               4    > JMP                                                      ->1
               5  > > RETURN                                                   1
      # do while loop
         3     0  >   ASSIGN                                                   !0, 0
         4     1  >   PRE_INC                                                  !0
               2      IS_SMALLER                                       ~2      !0, 1000000
               3    > JMPNZ                                                    ~2, ->1
         4        > > RETURN                                                   1
      # for loop
         3     0  >   ASSIGN                                                   !0, 0
               1  >   IS_SMALLER                                       ~1      !0, 1000000
               2    > JMPZNZ                                        5          ~1, ->6
               3  >   PRE_INC                                                  !0
               4    > JMP                                                      ->1
               5  > > JMP                                                      ->3
               6  > > RETURN                                                   1
      

      The do while loop only has one jump statement (JMPNZ), whereas the while loop needs two (JMPZ, JMP). The for loop needs three jump statements (JMPZNZ, JMP, JMP) and has generally more complex logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code with the if...else statement within a while loop. $colour
I have a page that is taking 37 seconds to load. While it is
I have an old C++ project I made a while back. Well, it is
I have a loop variable that does not appear to be getting garbage collected
I have a WPF App which is grinding to a halt after running out
I have put together a for loop for a small search function I am
Let's say you have a profile page that can only be accessed by the
I have created an application which needs to have a profile box tab which
Basically in order to have a profile Block u need - let's say profile
Hy, I have a profile x, that has a born_date and then i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.