Search code examples
phpoptimizationfunctionpass-by-referencepremature-optimization

passing a string by reference to a function would speed things up? (php)


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

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.

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;
}

?


Solution

  • 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.