|
Posted by Chameleon on 12/04/05 01:17
> example, if you time the following loops, you'll find that the second
> one is much slower:
>
> //------------------first
> $t = microtime(true);
> for($z = 0; $z < 1000000; $z++) {
> $b = $a;
> $c = $b;
> }
>
> //------------------second
> $t = microtime(true);
> for($z = 0; $z < 1000000; $z++) {
> $b = $a;
> $c =& $b;
> }
>
> Why is the second loop slow? That's because the reference assignment to
> $c forces a copy of the array to be made. The by-reference and by-value
> mechanism in PHP is quite unlike that in C/C++. By-value in PHP means
> share-read while by-reference means share-read-write. A variable cannot
> do both at the same time. If $c and $b are sharing read-write access,
> then $a and $b cannot be sharing just read access, as $a and $c are not
> sharing read-write access. A good analogy is Jack lending Jill a
> picture book. She is supposed to just read it. If she wants to give it
> to her little brother to draw on, she had better get her your copy,
> since that wasn't the agreement between her and Jack.
>
> If you can ensure that given piece of data is always passed
> by-reference, then you might gain some performance benefits. If
> somewhere along the line though, it was passed by-value, then you take
> a performance hit. That's one of the reason why in PHP 5 objects are
> always passed by-reference. Because invoking a method implies passing
> the object by reference ($this), objects should never be passed
> by-value.
>
indeed.
your explanation sounds logical.
but I believe this is wrong in php core.
why php must copy the object (in our script: array) even if the pointed
data don't change?
I believe it is simple to implement this in C (php core) and I have
opinion but its hard to my to explain in english:
object $a: array
object $b: read pointer to $a
object $c: pointer to object $b
$b is always in the same place in heap, so even if data of $b is a
pointer to $a or copied data of $a has no sense for $c. $c always points
to $b object.
And objects which point to another objects have a flag that means: read
or read-write.
I never look for php core code. I imagine about the structure of it. So
maybe I am totally wrong.
Simply I think it is a bad architecture of php core if without changing
data we have data copy.
[Back to original message]
|