通过引用传递一个字符串函数会加快速度吗? (php)[重复]

时间:2022-10-28 22:37:02

Possible Duplicate:
In PHP (>= 5.0), is passing by reference faster?

可能重复:在PHP(> = 5.0)中,更快地通过引用传递?

I wonder if by declaring the parameter pass by reference, the PHP interpreter will be faster for not having to copy the string to the function's local scope? The script turns XML files into CSVs, which have thousands of records, so little time optimizations count.

我想知道是否通过引用声明参数传递,PHP解释器将更快,因为不必将字符串复制到函数的本地范围?该脚本将XML文件转换为CSV,其中包含数千条记录,因此很少有时间优化。

Would this:


function escapeCSV( & $string )
{
    $string = str_replace( '"', '""', $string ); // escape every " with ""
    if( strpos( $string, ',' ) !== false )
        $string = '"'.$string.'"'; // if a field has a comma, enclose it with dobule quotes
    return $string;
}

Be faster than this:

要快于此:


function escapeCSV( $string )
{
    $string = str_replace( '"', '""', $string ); // escape every " with ""
    if( strpos( $string, ',' ) !== false )
        $string = '"'.$string.'"'; // if a field has a comma, enclose it with dobule quotes
    return $string;
}

?

4 个解决方案

#1


Don't think, profile.

不要想,个人资料。

Run scripts that use each version of the function for 100,000 repetitions under, say, the Unix time command. Do not philosophize about what is faster; find out.

在Unix时间命令下运行使用该函数的每个版本100,000次重复的脚本。不要对什么是更快的进行哲学思考;找出。

#2


My guess would be that this is a hardly noticeable optimization, that could potentially lead to unexpected sideeffects down the road. For example, the next guy is probably NOT going to expect escapeCSV to modify his parameter, and he might use the parameter he passed in thinking it is still the un-CSV'd string.

我的猜测是,这是一个难以察觉的优化,可能会导致意外的副作用。例如,下一个人可能不会期望escapeCSV修改他的参数,他可能会使用他传递的参数,认为它仍然是un-CSV'd字符串。

Also, since strings aren't edited in-place, you've already got two cases where a new string is created in your function (one for str_replace, one for the concatenation of quotes before/after it). One more for the pass-by-value case isn't going to be the bottleneck in your app.

此外,由于字符串不是就地编辑的,因此您已经有两种情况在您的函数中创建了一个新字符串(一个用于str_replace,一个用于连接之前/之后的引号)。对于按值传递的情况,还有一个不会成为您应用中的瓶颈。

#3


PHP references are not pointers. They do not speed anything up - In fact they do the contrary, since they require an additional internal entry in the symbol table.

PHP引用不是指针。它们不会加速任何事情 - 事实上它们恰恰相反,因为它们需要在符号表中添加额外的内部条目。

#4


[...] so little time optimizations count.

[...]这么少的时间优化很重要。

No, they don't.

不,他们没有。

The only true optimisation is the one that helps YOU read and/or write code faster. You should not sacrifice simplicity or readibility for performance - it will slow you down in long run.

唯一真正的优化是帮助您更快地读取和/或编写代码的优化。你不应该牺牲简单性或可读性来实现性能 - 从长远来看它会减慢你的速度。

Passing things by reference can be especially misleading. You may run into weird problems later, when some var will change mysteriously. You modify function input, which is not the way things normally work. Every time you do thing the unusual way you have to remember about them. Your memory and attention is limited though. Computer's is not. Don't overoptimise.

通过引用传递信息可能特别具有误导性。你可能会在以后遇到奇怪的问题,因为一些var会神秘地改变。您修改功能输入,这不是通常的工作方式。每当你以不寻常的方式做事时,你必须记住它们。你的记忆和注意力有限。电脑不是。不要过度优化。

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. --Brian W. Kernighan

调试的难度是首先编写代码的两倍。因此,如果您尽可能巧妙地编写代码,那么根据定义,您不够聪明,无法对其进行调试。 --Brian W. Kernighan

UPDATE

In this case your whole function escapeCSV is slightly pointless. You should use fputcsv, which is core PHP library written in C, thus is by far faster and memory efficient.

在这种情况下,你的整个函数escapeCSV略显无意义。你应该使用fputcsv,它是用C编写的核心PHP库,因此速度更快,内存效率更高。

#1


Don't think, profile.

不要想,个人资料。

Run scripts that use each version of the function for 100,000 repetitions under, say, the Unix time command. Do not philosophize about what is faster; find out.

在Unix时间命令下运行使用该函数的每个版本100,000次重复的脚本。不要对什么是更快的进行哲学思考;找出。

#2


My guess would be that this is a hardly noticeable optimization, that could potentially lead to unexpected sideeffects down the road. For example, the next guy is probably NOT going to expect escapeCSV to modify his parameter, and he might use the parameter he passed in thinking it is still the un-CSV'd string.

我的猜测是,这是一个难以察觉的优化,可能会导致意外的副作用。例如,下一个人可能不会期望escapeCSV修改他的参数,他可能会使用他传递的参数,认为它仍然是un-CSV'd字符串。

Also, since strings aren't edited in-place, you've already got two cases where a new string is created in your function (one for str_replace, one for the concatenation of quotes before/after it). One more for the pass-by-value case isn't going to be the bottleneck in your app.

此外,由于字符串不是就地编辑的,因此您已经有两种情况在您的函数中创建了一个新字符串(一个用于str_replace,一个用于连接之前/之后的引号)。对于按值传递的情况,还有一个不会成为您应用中的瓶颈。

#3


PHP references are not pointers. They do not speed anything up - In fact they do the contrary, since they require an additional internal entry in the symbol table.

PHP引用不是指针。它们不会加速任何事情 - 事实上它们恰恰相反,因为它们需要在符号表中添加额外的内部条目。

#4


[...] so little time optimizations count.

[...]这么少的时间优化很重要。

No, they don't.

不,他们没有。

The only true optimisation is the one that helps YOU read and/or write code faster. You should not sacrifice simplicity or readibility for performance - it will slow you down in long run.

唯一真正的优化是帮助您更快地读取和/或编写代码的优化。你不应该牺牲简单性或可读性来实现性能 - 从长远来看它会减慢你的速度。

Passing things by reference can be especially misleading. You may run into weird problems later, when some var will change mysteriously. You modify function input, which is not the way things normally work. Every time you do thing the unusual way you have to remember about them. Your memory and attention is limited though. Computer's is not. Don't overoptimise.

通过引用传递信息可能特别具有误导性。你可能会在以后遇到奇怪的问题,因为一些var会神秘地改变。您修改功能输入,这不是通常的工作方式。每当你以不寻常的方式做事时,你必须记住它们。你的记忆和注意力有限。电脑不是。不要过度优化。

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. --Brian W. Kernighan

调试的难度是首先编写代码的两倍。因此,如果您尽可能巧妙地编写代码,那么根据定义,您不够聪明,无法对其进行调试。 --Brian W. Kernighan

UPDATE

In this case your whole function escapeCSV is slightly pointless. You should use fputcsv, which is core PHP library written in C, thus is by far faster and memory efficient.

在这种情况下,你的整个函数escapeCSV略显无意义。你应该使用fputcsv,它是用C编写的核心PHP库,因此速度更快,内存效率更高。