You are here: Re: speed: by reference or by value? « PHP Programming Language « IT news, forums, messages
Re: speed: by reference or by value?

Posted by Berimor on 12/04/05 00:34

On 3 Dec 2005 13:28:55 -0800, Chung Leong <chernyshevsky@hotmail.com>
wrote:

> Chameleon wrote:
>> THE QUESTION:
>> If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
>> $v); )
>
> You should only use references when necessary, i.e. when you want the
> referenced variable to be modifed somewhere else down the line.
> Improper use of references will slow down your code considerably. For
> example, if you time the following loops, you'll find that the second
> one is much slower:
>
> //------------------first
> $t = microtime(true);
> for($z = 0; $z < 1000000; $z++) {
> $b = $a;
> $c = $b;
> }
>
> //------------------second
> $t = microtime(true);
> for($z = 0; $z < 1000000; $z++) {
> $b = $a;
> $c =& $b;
> }
>
> Why is the second loop slow?

To teach someone try it first by yourself!.
The first part (twice!!!!) slower then second one.
And do not compare C and PHP - its like to compare jet and aerostat.

Do try this code (but under php translator :) ) What will you say then?


<?
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}




$a =
array(42,345,3245,3245,2345,4564,51346,3457234,13762,4561,3465,23,451,345,32,45,23,45,23,4,76,3,45,14,6,45632,46,32,4);

$a['ddd'] = array(345,34,532,45,3245,3,45,2345,7643345,2645612,6423,562);

//------------------first
$time_start = getmicrotime();
for($z = 0; $z < 1000000; $z++) {
$b = & $a['ddd'];// <--------- THIS LINE
$b = $a;
$c = $b;
}


$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "$time\n ";

//------------------second
$time_start = getmicrotime();
for($z = 0; $z < 1000000; $z++) {
$b = $a;
$c = &$b;
}

$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "$time\n";

?>





--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация