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

  • SEARCH
  • Home
  • 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 739383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:23:39+00:00 2026-05-14T08:23:39+00:00

I have been running some small tests in PHP on loops. I do not

  • 0

I have been running some small tests in PHP on loops. I do not know if my method is good.

I have found that a inverse loop is faster than a normal loop.

I have also found that a while-loop is faster than a for-loop.

Setup

<?php

$counter = 10000000;
$w=0;$x=0;$y=0;$z=0;
$wstart=0;$xstart=0;$ystart=0;$zstart=0;
$wend=0;$xend=0;$yend=0;$zend=0;

$wstart = microtime(true);
for($w=0; $w<$counter; $w++){
    echo '';
}
$wend = microtime(true);
echo "normal for: " . ($wend - $wstart) . "<br />";

$xstart = microtime(true);
for($x=$counter; $x>0; $x--){
    echo '';
}
$xend = microtime(true);
echo "inverse for: " . ($xend - $xstart) . "<br />";

echo "<hr> normal - inverse: " 
        . (($wend - $wstart) - ($xend - $xstart)) 
        . "<hr>";

$ystart = microtime(true);
$y=0;
while($y<$counter){
    echo '';
    $y++;
}
$yend = microtime(true);
echo "normal while: " . ($yend - $ystart) . "<br />";

$zstart = microtime(true);
$z=$counter;
while($z>0){
    echo '';
    $z--;
}
$zend = microtime(true);
echo "inverse while: " . ($zend - $zstart) . "<br />";

echo "<hr> normal - inverse: " 
        . (($yend - $ystart) - ($zend - $zstart)) 
        . "<hr>";

echo "<hr> inverse for - inverse while: " 
        . (($xend - $xstart) - ($zend - $zstart))
        . "<hr>";
?>

Average Results

The difference in for-loop

normal for: 1.0908501148224
inverse for: 1.0212800502777

normal – inverse: 0.069570064544678

The difference in while-loop

normal while: 1.0395669937134
inverse while: 0.99321985244751
normal – inverse: 0.046347141265869

The difference in for-loop and while-loop

inverse for – inverse while: 0.0280601978302

Questions

My question is can someone explain these differences in results?
And is my method of benchmarking been correct?

  • 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-14T08:23:39+00:00Added an answer on May 14, 2026 at 8:23 am

    With the inverse for loop, you’re only doing one variable lookup per iteration:

    $w > 0         // <-- one lookup to the $w variable
    
    $w < $counter  // <-- two lookups, one for $w, one for $counter
    

    This is why the inverse is slightly faster. Also, a while loop has only one operation per iteration:

    $w < $counter        // <-- one operation while loop
    
    $w < $counter ; $w++ // <-- two operation for loop
    

    Of course, you have that extra operation inside the loop’s code block, but I’m not sure exactly why that’s faster (maybe someone can fill in the blank there). You’ll notice the time difference is minimal, because these operations are still very fast. Such micro-optimisations are most effective on very large loops.

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

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Thanks to everone who took the time to make a… May 14, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer Why not have your script copy the file to the… May 14, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer Looks like Django is not available in any of the… May 14, 2026 at 4:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.