473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

speed: by reference or by value?

-------
$a = array(............);
$b = $a;
-------
php says that $b points to $a until $b or $a changes.
this means that the code above wants the same time with the code below:
-------
$a = $a = array(............);
$b = & $a;
-------

but when I check this with a script, I found this:
when I use & (ref) the script is faster.

in the code below: (php 5. WinXP. command line script)
first part needs 2.9 secs
second part (in "this line" & exists) needs 3.0 secs
second part (in "this line" & not exist) needs 6.5 secs but not $a nor
$b changes. So I believe that php creates a whole copy of $a in $b.

THE QUESTION:
If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
$v); )

---------THE CODE------------------------------------------
<?
$a =
array(42,345,3245,3245,2345,4564,51346,3457234,137 62,4561,3465,23,451,345,32,45,23,45,23,4,76,3,45,1 4,6,45632,46,32,4);

$a['ddd'] = array(345,34,532,45,3245,3,45,2345,7643345,2645612 ,6423,562);

//------------------first
$t = microtime(true);
for($z = 0; $z < 1000000; $z++) {
foreach($a['ddd'] as $v);
}
$t = microtime(true) - $t;
echo "$t\n";
//------------------second
$t = microtime(true);
for($z = 0; $z < 1000000; $z++) {
$b = & $a['ddd']; // <--------- THIS LINE
foreach($b as $v);
}
$t = microtime(true) - $t;
echo "$t\n";
?>
----------------------------------------------------
Dec 3 '05 #1
11 1370
Chameleon wrote:
-------
$a = array(............);
$b = $a;
-------
php says that $b points to $a until $b or $a changes.
this means that the code above wants the same time with the code below:
------- $a = $a = array(............);
error: I mean
$a = array(............);
$b = & $a;
-------

Dec 3 '05 #2
On Sat, 03 Dec 2005 19:22:29 +0200, Chameleon
<ch******@hotmail.NOSPAM.com> wrote:
-------
$a = array(............);
$b = $a;
-------
php says that $b points to $a until $b or $a changes.
this means that the code above wants the same time with the code below:
-------
$a = $a = array(............);
$b = & $a;
-------

but when I check this with a script, I found this:
when I use & (ref) the script is faster.

in the code below: (php 5. WinXP. command line script)
first part needs 2.9 secs
second part (in "this line" & exists) needs 3.0 secs
second part (in "this line" & not exist) needs 6.5 secs but not $a nor
$b changes. So I believe that php creates a whole copy of $a in $b.

THE QUESTION:
If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
$v); )

---------THE CODE------------------------------------------
<?
$a =
array(42,345,3245,3245,2345,4564,51346,3457234,137 62,4561,3465,23,451,345,32,45,23,45,23,4,76,3,45,1 4,6,45632,46,32,4);

$a['ddd'] = array(345,34,532,45,3245,3,45,2345,7643345,2645612 ,6423,562);

//------------------first
$t = microtime(true);
for($z = 0; $z < 1000000; $z++) {
foreach($a['ddd'] as $v);
}
$t = microtime(true) - $t;
echo "$t\n";
//------------------second
$t = microtime(true);
for($z = 0; $z < 1000000; $z++) {
$b = & $a['ddd']; // <--------- THIS LINE
foreach($b as $v);
}
$t = microtime(true) - $t;
echo "$t\n";
?>
----------------------------------------------------


No, you're wrong in your research. The reson is very unnoing though. You
treat the microtime() function wrong. Please read PHP manual - beacuse it
can hurt you once. My fixings shown that second part is really faster -
but only about 8% then first one.
Here is code:

<?
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

$a =
array(42,345,3245,3245,2345,4564,51346,3457234,137 62,4561,3465,23,451,345,32,45,23,45,23,4,76,3,45,1 4,6,45632,46,32,4);

$a['ddd'] = array(345,34,532,45,3245,3,45,2345,7643345,2645612 ,6423,562);

//------------------first
$time_start = getmicrotime();
for($z = 0; $z < 1000000; $z++) {
$b = & $a['ddd'];// <--------- THIS LINE
foreach($b as $v);
}
$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "$time\n ";
//------------------second
$time_start = getmicrotime();
for($z = 0; $z < 1000000; $z++) {
foreach($a['ddd'] as $v);
}

$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "$time\n";


?>

--

Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk
Dec 3 '05 #3
Chameleon wrote:
THE QUESTION:
If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
$v); )


You should only use references when necessary, i.e. when you want the
referenced variable to be modifed somewhere else down the line.
Improper use of references will slow down your code considerably. For
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.

Dec 3 '05 #4
It's oft imprudent to tell others to read the manual without reading it
first ;-)

Dec 3 '05 #5
On 3 Dec 2005 13:41:09 -0800, Chung Leong <ch***********@hotmail.com>
wrote:
It's oft imprudent to tell others to read the manual without reading it
first ;-)

Explain please.
--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk
Dec 3 '05 #6
See http://fi.php.net/microtime. Note the new parameter available for
PHP 5.

Dec 3 '05 #7
On 3 Dec 2005 13:28:55 -0800, Chung Leong <ch***********@hotmail.com>
wrote:
Chameleon wrote:
THE QUESTION:
If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
$v); )


You should only use references when necessary, i.e. when you want the
referenced variable to be modifed somewhere else down the line.
Improper use of references will slow down your code considerably. For
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?


To teach someone try it first by yourself!.
The first part (twice!!!!) slower then second one.
And do not compare C and PHP - its like to compare jet and aerostat.

Do try this code (but under php translator :) ) What will you say then?
<?
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}


$a =
array(42,345,3245,3245,2345,4564,51346,3457234,137 62,4561,3465,23,451,345,32,45,23,45,23,4,76,3,45,1 4,6,45632,46,32,4);

$a['ddd'] = array(345,34,532,45,3245,3,45,2345,7643345,2645612 ,6423,562);

//------------------first
$time_start = getmicrotime();
for($z = 0; $z < 1000000; $z++) {
$b = & $a['ddd'];// <--------- THIS LINE
$b = $a;
$c = $b;
}
$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "$time\n ";

//------------------second
$time_start = getmicrotime();
for($z = 0; $z < 1000000; $z++) {
$b = $a;
$c = &$b;
}

$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "$time\n";

?>

--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk
Dec 3 '05 #8
On 3 Dec 2005 14:28:21 -0800, Chung Leong <ch***********@hotmail.com>
wrote:
See http://fi.php.net/microtime. Note the new parameter available for
PHP 5.


hmmm... Really. Well it's always hard to leave lovely things.



--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk
Dec 3 '05 #9
ah sorry,
shame on me :(



--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk
Dec 3 '05 #10
> 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.
Dec 3 '05 #11
Chameleon wrote:
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?


Perhaps it's possible to defer the copying until the write operation
actually, but I suspect the logic would be quite complicated. It's like
Jill trying into slip a new picture book under the hand of her little
brother start as he starts drawing.

But as I said, the use of reference usually denotes that changes will
occur. If you don't want variable separation to occur, then don't use
references.

Dec 4 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,...
10
by: Fabian | last post by:
Are there any speed issues in javascript with having really large arrays? I know if the array gets large enough, bandwidth and download time can be an issue, but does it take inordinate amounts of...
4
by: Michael | last post by:
If I am going to be using the same class over and over again passing between functions, would the following be quicker (as the constructor wouldn't have to be called): //First Example: class...
9
by: Mario Lüttich | last post by:
Hi this code: > float tmp = 0.; > for (int i = 0; i < 2000; i++) { > for (int j = i; j < 2000; j++) { > for (int k = 0; k < 9000; k++) { > tmp += tmp_mat(k,i) * tmp_mat(k,j);
0
by: Mike | last post by:
What type of collection and what datatypes would be most efficient for storing the following data considering both memory and speed? Is there a formula to calculate the memory usage and estimate...
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
19
by: vunet.us | last post by:
Hello, My AJAX application paints data into about 500 cells with unique ID every 10 seconds. I am using document.getElementById() to find the right cell. However, I have noticed that...
11
by: blackx | last post by:
I'm using clock() to measure the speed of my code (testing the speed of passing by value vs passing by reference in function calls). The problem is, the speed returned by my code is always 0.0000000...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.