On Wed, 2008-07-23 at 10:07 +0200, Yeti wrote:
> Hello everyone,
> 
> Many of you may be familiar with references in PHP. Now i read
> somewhere in the PHP manual that creating references can take longer
> than copies. PHP5+ also seems to reference a bit different thant PHP4
> did.
> Here is some working example code:
> 
> <?php
> class useless {
>       var $huge_array;
>       function __construct() {
>               $this->huge_array = array();
>               for ($i = 0; $i < 1024; $i++) $this->huge_array[] = $GLOBALS; //
> fill ze array with copies of $GLOBALS array
>               return true;
>       }
>       function useless() {
>               return $this->__construct();
>       }
> }
> $time_start = microtime(true);
> $test_obj = new useless();
> $time_end = microtime(true);
> $time = $time_end - $time_start;
> echo "It took {$time} seconds without using the reference operator.\r\n";
> unset($test_obj);
> $time_start = microtime(true);
> $test_obj =& new useless();
> $time_end = microtime(true);
> $time = $time_end - $time_start;
> echo "It took {$time} seconds using the reference
> operator.\r\n########## with obj2 ############\r\n";
> $time_start = microtime(true);
> $test_obj = new useless();
> $obj2 = $test_obj;
> $time_end = microtime(true);
> $time = $time_end - $time_start;
> echo "It took {$time} seconds without using the reference operator.\r\n";
> unset($test_obj);
> $time_start = microtime(true);
> $test_obj =& new useless();
> $obj2 =& $test_obj;
> $time_end = microtime(true);
> $time = $time_end - $time_start;
> echo "It took {$time} seconds using the reference operator.\r\n";
> ?>
> 
> I tested the code in PHP 4.4.7 and in PHP 5.2.5 and the results were
> pretty much the same. Using references speeds up the script!
> Occasionally obj2-with-references took longer than all the others. But
> i don't know if that's to be taken serious.
> 
> Now if i do not need a copy, isn't it smarter to use references instead?
> 
> I'm grateful for any ideas, thoughts or experiences

In PHP4 if you don't need a copy then use a reference... it will be
faster. In PHP5 you don't get a copy unless you explicitly clone the
object. And so, in PHP5 assignment by value is faster than assignment by
reference. However, there may be the odd time you really want a
reference. PHP4 is dead though... so they say.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to