note 39671 modified in language.operators.increment by aidan
| From: | aidan@php.net | Date: | Thu, 26 Aug 2004 23:17:53 +0000 |
| Subject: | note 39671 modified in language.operators.increment by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75390@lists.php.net to get a copy of this message | ||
Interesting performance note:
$i++ seems to be slightly slower than ++$i, when used on a line by itself the 2 have the same
purpose. It's not much, but over 100,000 incements the pre-increment is about .004 seconds
faster on average.
--was--
Interesting performance note:
$i++ seems to be slightly slower than ++$i, when used on a line by itself the 2 have the same
purpose. It's not much, but over 100,000 incements the pre-increment is about .004 seconds
faster on average.
<?
$start = 0;
$stop = 0;
$post = array();
$pre = array();
function perfgetmicrotime()
{
list($usec, $sec) = explode(' ',microtime());
return ((float)$usec + (float)$sec);
}
//preload the variable to allocate the memory so the test will be fair for both
$i = 0;
while ($i < 100000)
{
$i++;
}
//start post test
for($j = 0 ; $j < 100 ; $j++)
{
$i = 0;
$start = perfgetmicrotime();
while ($i < 100000)
{
$i++;
}
$stop = perfgetmicrotime();
$post[] = $stop - $start;
}
echo "Post average: ".(array_sum($post)/count($post))."\n";
//start pre test
for($j = 0 ; $j < 100 ; $j++)
{
$i = 0;
$start = perfgetmicrotime();
while ($i < 100000)
{
++$i;
}
$stop = perfgetmicrotime();
$pre[] = $stop - $start;
}
echo "Pre average: ".(array_sum($pre)/count($pre))."\n";
?>
http://php.net/manual/en/language.operators.increment.php