Aaron Saray

open source programmer,
web developer

entrepreneur, author
and musician

My Blog

contains PHP, Web and business/entrepreneurial related content. Please join in the conversation!

Is it better to write your 'for' loops backwards in PHP?

After listening to a javascript internals optimization talk, I wanted to see how these concepts could relate to PHP. The biggest thing that stuck out to me was the order of the for loops in javascript. According to the talk, loops written backwards (or using the deincrement operator instead of the increment one…) was a lot faster. They said comparing a value to zero was faster than comparing a value to another value. With the backward loop, you were always comparing to zero. I decided to try these tests on php:

I’m not entirely solid with my testing method. My idea was to make a forward loop and a backward loop. Each would do one PHP action. I’d run the test 10 times to get an idea what the mean was – did forward loops win or did backward ones win? This is the code I used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
$var = 1;

for ($j=0; $j < 10; $j++) {
    $time_start = microtime(true);
    for ($i=0; $i < 100000; $i++) {
        $var = 1;
    }
    $time_end = microtime(true);
    $timef = $time_end - $time_start;
    print "{$timef} - forward<br />";


    $time_start = microtime(true);
    for ($i=100000; 0 < $i; $i--) {
        $var = 1;
    }
    $time_end = microtime(true);
    $timeb = $time_end - $time_start;
    print "{$timeb} - backward<br />";


    if ($timef > $timeb) {
        print '<strong>Backward Wins</strong>';
    }
    else if ($timef < $timeb) {
        print '<strong>Forward Wins</strong>';
    }
    else {
        print 'ARE YOU KIDDING ME?';
    }
    print '<br /><hr />';
}
?>

After running the test a few times, I was actually surprised. There really wasn’t that much of a difference. Here’s an example of the output for one of my tests:

0.061929941177368 – forward 0.064308881759644 – backward Forward Wins


0.069388151168823 – forward 0.056493997573853 – backward Backward Wins


0.065435886383057 – forward 0.056330919265747 – backward Backward Wins


0.065697908401489 – forward 0.056133031845093 – backward Backward Wins


0.066594123840332 – forward 0.065793991088867 – backward Backward Wins


0.059870004653931 – forward 0.064540863037109 – backward Forward Wins


0.057721138000488 – forward 0.064589023590088 – backward Forward Wins


0.057284832000732 – forward 0.064239978790283 – backward Forward Wins


0.067974090576172 – forward 0.056583166122437 – backward Backward Wins


0.068339824676514 – forward 0.056351900100708 – backward Backward Wins


So as you can see, there’s no real proof that in PHP the order of your for loop matters.

This entry was posted in PHP and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>