[thelist] PHP syntax problem

Hans Zaunere zaunere at yahoo.com
Wed Apr 17 11:17:00 CDT 2002


References (aliases) in PHP is a great and confusing topic, and has
been a thorn in my side for some time.  Coming from C, I considered
references to be pointers.  After spending countless hours trying to
optimize my PHP code and haggle people the "best" way to use references
to squeez every last drop of memory/performance out of my code, I was
told, rather bluntly, that references in PHP shouldn't be called
references, but rather aliases.  Because of previous versions of PHP,
the term references has stuck around, when in fact it is nothing like
that of C (passing by reference or value ie pointers).

These two pages have helped me greatly:
http://www.zend.com/zend/whats-new.php
http://www.zend.com/zend/art/ref-count.php

Unfortunetly, the other articles and docs I've seen completely miss the
point of the above article.

That said, there are some tricks with objects, of which I am not 100%
sure about (as I don't use them very often).  I know that in the
constructor of an object, two objects are created, and one destroyed.
There are some other tricky aspects with objects (of which Andi only
touches on in the article).

However, the code provided below does not show that using a reference
with the new operator makes any difference.  Take the code below and
swap the alias signs (&) between the two for() loops  - you'll notice
no difference, when you'd expect there should be.  Or, use alias signs
in both for() loops - same thing.  Basically, PHP (Zend engine,
actually) is caching and reference counting, make the expense the same,
with, or without the alias sign.

This is certainly a topic of confusion and ongoing debate.  I think
with the next release of the Zend Engine, this will be better
explained.  However, I still have found no performance/memory problems
sticking to the simple rule:  only use references (aliases actually!)
when you need to modify the original value.

Hans Z.
New York PHP
http://nyphp.org

--- Andy Warwick <andy.w at creed.co.uk> wrote:
> After a little more experimentation, and using the following timing
> script, I'm
> getting between 10 and 40% improvement in speed assigning with
> reference as
> compared to without.
>
> YMMV
>
> Andy
>
> ----
> <?php
>
> class myClass
> {
>     var $datavar;
>
>     function myClass()
>     {
>         $GLOBALS['ref_to_myObj'] = & $this; // create a reference to
> this obj so
> it doesn't get destroyed
>
>         $this->datavar = "This is the data for the first object.";
>     }
> }
>
> $startTime = microtime ( ) ;
>
> for ( $x = 1; $x <= 10000; $x++ )
> {
>     $myObj = new myClass();
>     unset($myObj);
> }
>
> $endTime = microtime ( ) ;
>
> $without_assignment_speed = execute_time($startTime, $endTime) ;
>
> echo "Create 10000 objects without assignment by reference =
> ".$without_assignment_speed." sec<br />\n" ;
>
> $startTime = microtime ( ) ;
>
> for ( $x = 1; $x <= 10000; $x++ )
> {
>     $myObj = &new myClass();
>     unset($myObj);
> }
>
> $endTime = microtime ( ) ;
>
> $with_assignment_speed = execute_time($startTime, $endTime) ;
>
> echo "Create 10000 objects with assignment by reference =
> ".$with_assignment_speed." sec<br /><br />\n" ;
>
> $speed_increase = ( ($without_assignment_speed -
> $with_assignment_speed) /
> $with_assignment_speed) * 100 ;
>
> echo "By assignment is ".number_format(($speed_increase),1)."%
> faster." ;
>
> function execute_time ($start, $end)
> {
>     $timeparts = explode(" ",$start);
>     $starttime = $timeparts[1].substr($timeparts[0],1);
>     $timeparts = explode(" ",$end);
>     $endtime = $timeparts[1].substr($timeparts[0],1);
>     return number_format(($endtime-$starttime),4);
> }
>
> ?>


__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/



More information about the thelist mailing list