Reply to Re: speed: by reference or by value?

Your name:

Reply:


Posted by Berimor on 12/03/05 23:24

On Sat, 03 Dec 2005 19:22:29 +0200, Chameleon
<cham_gss@hotmail.NOSPAM.com> wrote:

> -------
> $a = array(............);
> $b = $a;
> -------
> php says that $b points to $a until $b or $a changes.
> this means that the code above wants the same time with the code below:
> -------
> $a = $a = array(............);
> $b = & $a;
> -------
>
> but when I check this with a script, I found this:
> when I use & (ref) the script is faster.
>
> in the code below: (php 5. WinXP. command line script)
> first part needs 2.9 secs
> second part (in "this line" & exists) needs 3.0 secs
> second part (in "this line" & not exist) needs 6.5 secs but not $a nor
> $b changes. So I believe that php creates a whole copy of $a in $b.
>
> THE QUESTION:
> If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
> $v); )
>
> ---------THE CODE------------------------------------------
> <?
> $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
> $t = microtime(true);
> for($z = 0; $z < 1000000; $z++) {
> foreach($a['ddd'] as $v);
> }
> $t = microtime(true) - $t;
> echo "$t\n";
> //------------------second
> $t = microtime(true);
> for($z = 0; $z < 1000000; $z++) {
> $b = & $a['ddd']; // <--------- THIS LINE
> foreach($b as $v);
> }
> $t = microtime(true) - $t;
> echo "$t\n";
> ?>
> ----------------------------------------------------



No, you're wrong in your research. The reson is very unnoing though. You
treat the microtime() function wrong. Please read PHP manual - beacuse it
can hurt you once. My fixings shown that second part is really faster -
but only about 8% then first one.
Here is code:

<?
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
foreach($b as $v);
}


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

echo "$time\n ";
//------------------second
$time_start = getmicrotime();
for($z = 0; $z < 1000000; $z++) {
foreach($a['ddd'] as $v);
}

$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

[Back to original 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

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