From: Robert Cummings on
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

From: Ted Wood on

The general rules of thumb are --> don't use references unless you
actually *want* a reference. And don't use references for performance
reasons.

Under PHP 4, it's generally been recommended to use a reference
operator when creating objects.

$obj =& new Object();

PHP uses references internally until it's necessary to create a copy:

$a = array();

$a = $b; // internally, $b is a reference to $a

$b[] = "chocolate"; // at this point, a copy is made, and $b is
modified

So PHP waits until a copy is actually needed before it makes one.
Explicitly making copies incurs overhead because of the concept of
"reference counting". So again, don't use references for performance
reasons.

~Ted




On 23-Jul-08, at 1:07 AM, 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
> Yeti
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

From: Ted Wood on

On 23-Jul-08, at 1:19 AM, Ted Wood wrote:
>
> So PHP waits until a copy is actually needed before it makes one.
> Explicitly making copies incurs overhead because of the concept of
> "reference counting". So again, don't use references for performance
> reasons.


That should've been: Explicitly making "references", not copies...

~Ted
From: Yeti on
<?php
# So if i want a copy in PHP, like:

$a = $b;

# In PHP5 $a would still be a reference as long as $b is not being changed?(!)
# Such behaviour makes it extremely easy to write a speedy script
without worrying about copy/reference issues!

# Thanks to all of you,
# Ernie
?>
From: Ted Wood on

In both PHP 4 and PHP 5, this would be an internal reference:

$a = $b;

until $b is changed.

The only exception is with Object handling under PHP 4, which had
flawed reference handling.

So, the rule remains -- don't use references for performance reasons
-- use them if you need references. :-)


~Ted



On 23-Jul-08, at 1:20 PM, Yeti wrote:

> <?php
> # So if i want a copy in PHP, like:
>
> $a = $b;
>
> # In PHP5 $a would still be a reference as long as $b is not being
> changed?(!)
> # Such behaviour makes it extremely easy to write a speedy script
> without worrying about copy/reference issues!
>
> # Thanks to all of you,
> # Ernie
> ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>