[nycphp-talk] Accessing non-existent array elements ok?
Rolan Yang
rolan at omnistep.com
Tue Aug 19 10:21:48 EDT 2008
David Mintz wrote:
>
>
> I heard somewhere that it does cost to reference/de-reference for no
> good reason, and that if youi're not going to write to it, pass it by
> value not reference. But I don't remember when or where, so don't ask
> me support that with things like facts or sources.
>
> --
> David Mintz
> http://davidmintz.org/
>
Wow, what a shocker.
I just ran a quick test and passing by reference is SLOWER by a factor
of 1000.
I'm running the script below on a AMD Athlon XP 2200+ with 1GB RAM
Redhat Fedora Core 5.
(if the email is mangled, you can grab the source from
http://omnistep.com/phpstuff/ )
You need to supply a reasonably large file and repoint the $filename
variable to it before running.
<?php
// Tests speed of passing by reference vs passing by value
// Rolan Yang rolan at omnistep.com Aug 19, 2008
$filename='os-php-cake2-ltr.pdf'; // point this to any fairly large
file (1+ Megabyte?)
$blobofdata=file($filename); // load blob of data into a variable
if (isset($_SERVER['HTTP_USER_AGENT'])) {$newline="<br>\n";} else
{$newline="\n";}
$start=microtime(TRUE);
print "Pass by Reference{$newline}";
print "Start: $start{$newline}";
for ($x=0;$x<500;$x++) {
$a=passByRef($blobofdata); // passes it by reference 500 times
}
$end=microtime(TRUE);
print "End: $end{$newline}Total=".($end-$start).$newline.$newline;
$start=$end;
print "Pass by Value{$newline}";
print "Start: $start{$newline}";
for ($x=0;$x<500;$x++) {
$a=passByValue($blobofdata); // passes it by value 500 times
}
$end=microtime(TRUE);
print "End: $end{$newline}Total=".($end-$start).$newline;
function passByRef(&$bla) {
return $bla;
}
function passByValue($bla) {
return $bla;
}
?>
More information about the talk
mailing list