简体   繁体   English

php数组引用传递给函数

[英]php array reference passing to function

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] ) 我正在构建一个包含文件十六进制值的非常大的数组(如$array[0]=B5 $array[1]=82 ,依此类推,直到$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) { ... } ) .. 当我创建一个函数来操作该数组中的一些偏移并传递$array作为引用( function parse(&$array) { ... } )时,它比我正常传递数组需要更长的时间( function parse($array) { ... } )..

How is that possible ? 怎么可能?

PS: Is there any faster way not to use array at all ? PS:有没有更快的方法不使用阵列? 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" 只是使用$ string =“B5 82 00 1E ..etc”,但我需要跟踪偏移量,因为我提前读取十六进制值,因为其中一些值包含长度“

There are some informations that might help in the following article : Do not use PHP references 以下文章中有一些可能有用的信息: 不要使用PHP引用

Close to the end of that post, Johannes posts the following portion of code (quoting) : 接近该帖子的末尾,Johannes发布了以下代码部分(引用)

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. 但是, strlen()需要一份副本。
copy-on-write can't be done on references so $data will be copied for calling strlen() , strlen() will do an absolutely simple operation - in fact strlen() is one of the most trivial functions in PHP - and the copy will be destroyed immediately. copy-on-write无法在引用上完成,所以$data将被复制用于调用strlen()strlen()将执行一个绝对简单的操作- 实际上strlen()是PHP中最简单的函数之一 -并且副本将立即销毁。

You might well be in a situation like this one, considering the question you are asking... 考虑到你问的问题,你可能会遇到这样的情况......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM