Reputation: 2930
I have a question that I cannot find an answer.
I'm constructing a very big array that contain hex values of a file (like $array[0]=B5
$array[1]=82
and so on until $array[1230009]
)
When I create a function to manipulate some offsets in that array and pass the $array
as reference ( function parse(&$array) { ... }
) it takes WAY longer than if I pass the array normaly ( function parse($array) { ... }
) ..
How is that possible ?
PS: Is there any faster way not to use array at all ? Just to use a $string = "B5 82 00 1E ..etc", but I need to track the Offset as i advance in reading hex values as some of this values contains lengths"
Upvotes: 6
Views: 2145
Reputation: 400932
There are some informations that might help in the following article : Do not use PHP references
Close to the end of that post, Johannes posts the following portion of code (quoting) :
function foo(&$data) {
for ($i = 0; $i < strlen($data); $i++) {
do_something($data{$i});
}
}
$string = "... looooong string with lots of data .....";
foo(string);
And a part of the comment that goes with it is (still quoting) :
But now in this case the developer tried to be smart and save time by passing a reference.
But well,strlen()
expects a copy.
copy-on-write can't be done on references so$data
will be copied for callingstrlen()
,strlen()
will do an absolutely simple operation - in factstrlen()
is one of the most trivial functions in PHP - and the copy will be destroyed immediately.
You might well be in a situation like this one, considering the question you are asking...
Upvotes: 3