[MUST READ] The Fastest For (Improve your PHP!)
Posted: Fri Oct 20, 2006 9:51 pm
My Fellow Programmers
I thank you for spending the time to read this article. I will must-likely in the future rewrite this but currently I'm not interested. Well your reading this because ether the title caught your eye or you want faster PHP code; I can do that.
Earlier today, me and a college buddy were conversing about which for statements were faster. So I built a little PHP script to prove that my statement was correct. You should really try this out. By using the statement for ($i = 10000000; $i > 0; --$i) instead of for ($i = 0; $i < 10000000; $i++) there is a nice speed improvement. Basically, the test is saying computers count down better then they count up. (Does gravity have anything to do with it? lol)
Then this got me thinking, what about for and foreach statements which is faster? First I tried the for ($i=0; $i < count($array); $i++) with foreach ($array as $arr). I noticed that foreach was insanely faster by almost a whole second. Wondering even more I tried the the same for loop but using the faster methoud counting down as such: for ($i=count($array); $i > 0; --$i). This was still faster then the traditional for statement but slower then the foreach.
Code Version: 1.05
My results may be testing using this script I formulated:
P.S. If you wish for me to add more to the script tell me and I'll update it.
I thank you for spending the time to read this article. I will must-likely in the future rewrite this but currently I'm not interested. Well your reading this because ether the title caught your eye or you want faster PHP code; I can do that.
Earlier today, me and a college buddy were conversing about which for statements were faster. So I built a little PHP script to prove that my statement was correct. You should really try this out. By using the statement for ($i = 10000000; $i > 0; --$i) instead of for ($i = 0; $i < 10000000; $i++) there is a nice speed improvement. Basically, the test is saying computers count down better then they count up. (Does gravity have anything to do with it? lol)
Then this got me thinking, what about for and foreach statements which is faster? First I tried the for ($i=0; $i < count($array); $i++) with foreach ($array as $arr). I noticed that foreach was insanely faster by almost a whole second. Wondering even more I tried the the same for loop but using the faster methoud counting down as such: for ($i=count($array); $i > 0; --$i). This was still faster then the traditional for statement but slower then the foreach.
Code Version: 1.05
My results may be testing using this script I formulated:
Code: Select all
<?php
/**
* For statement analyzer 1.05
* Programmed by David Branco <David@NeoeliteUSA.com>
* http://www.NeoeliteUSA.com
* October 21, 2006
*/
/* Timer Class to measure the speed of PHP */
class Timer {
private static $page_generate_start;
private static $page_generate_end;
public static $page_generate_time;
function measure()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function start()
{
self::$page_generate_start = self::measure();
return true;
}
function end()
{
self::$page_generate_end = self::measure();
self::$page_generate_time = self::$page_generate_end - self::$page_generate_start;
return true;
}
}
$timer = new Timer();
timer::start();
for ($i = 0; $i < 10000000; $i++) {}
timer::end();
echo 'for ($i = 0; $i < 10000000; $i++)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
for ($i = 0; $i < 10000000; ++$i) {}
timer::end();
echo 'for ($i = 0; $i < 10000000; ++$i)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
for ($i = 10000000-1; $i >= 0; --$i) {}
timer::end();
echo 'for ($i = 10000000-1; $i >= 0; --$i)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
for ($i = 10000000-1; $i >= 0; $i--) {}
timer::end();
echo 'for ($i = 10000000-1; $i >= 0; $i--)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
for ($i = 10000000; $i > 0; --$i) {}
timer::end();
echo 'for ($i = 10000000; $i > 0; --$i)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
for ($i = 10000000; $i > 0; $i--) {}
timer::end();
echo 'for ($i = 10000000; $i > 0; $i--)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
/***************************************************************/
/***************************************************************/
/***************************************************************/
/***************************************************************/
/***************************************************************/
/***************************************************************/
function printr ( $object , $name = '' ) {
if ( is_array ( $object ) ) {
print ( '<pre>' ) ;
print_r ( $object ) ;
print ( '</pre>' ) ;
} else {
var_dump ( $object ) ;
}
}
$array = array('a','b','c','d','e','f','g','u','k','n','o','p');
echo printr($array)."\n";
timer::start();
$total=count($array);
for ($i=0; $i<$total; $i++) {}
timer::end();
echo 'Traditional for statment: for ($i=0; $i<$total; $i++)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
for ($i=0, $total=count($array); $i<$total; $i++) {}
timer::end();
echo 'Wei Style for statment: for ($i=0, $total=count($array); $i<$total; $i++)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
for ($i=count($array); $i > 0; --$i) {}
timer::end();
echo 'Fastest for statment: for ($i=count($array); $i > 0; --$i)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
timer::start();
foreach ($array as $arr) {}
timer::end();
echo 'foreach statment: foreach ($array as $arr)'.'<br />'.timer::$page_generate_time.'<br /><br />'."\n";
?>