|
Posted by "Satyam" on 10/13/01 11:25
<tg-php@gryffyndevelopment.com> wrote in message
news:E1EAUy8-0006ZH-PF@www11.dixiesys.com...
> To elaborate on Philip's response (which is correct)...
[....]
>
>
> I've read "never use double quotes unless you have to" because it could
> potentially speed up PHP a little because it won't be trying to interpret
> every string.
I once did some trials with thousand repetitions of segments of code with
different alternatives, so let me go through all of them since I have the
numbers anyway. The original was in Spanish so I'll just give the results.
The question started with using echo or print and, as the manual says, echo
is, indeed, faster:
echo "uno $f tres ": 3.04
print "uno $f tres ": 3.70
The numbers after the colons are the execution times of one against the
other for a lot of repetitions, anyhow, they should be read as relative to
one another.
Next, most people don't know that echo accepts multiple arguments separated
by commas. Separating the arguments with commas is faster.
echo 'uno ' . ' dos ' . ' tres ': 1.32
echo 'uno ' , ' dos ' , ' tres ': 0.94
In fact, this is a poor example since the difference gets larger with longer
string and more arguments. When you use dots, the interpreter has to
actually concatenate the string, looking for memory to do so and freeing it
up afterwards. This takes time. With commas, each argument is sent to the
output stream as soon as it is found, no further processing is needed in
between.
Then the single vs. double quotes:
echo 'uno ' , ' dos ' , ' tres ': 0.94
echo "uno " , " dos " , " tres ": 6.76
Once again, these are relative times but it means using double quotes is 7
times slower than single quotes
Several different alternatives with variables involved:
echo 'uno ' . $f . ' tres ': 7.38
echo 'uno ' , $f , ' tres ': 0.80
echo "uno $f tres ": 3.04
echo <<<EOT
uno $f tres
EOT: 3.29
Notice that when variables are involved, the difference in between echoing
with arguments separated with commas and separated with dots is more than 9
times faster for the commas. Using double quotes with variable expansion
is almost 4 times slower than the commas, but is still faster than
concatenating them externaly with dots. Using heredoc-style strings is not
so bad compared to double quotes.
So, if you are sending out the rule would be:
Use echo, not print. Separate arguments with commas.
Now, if you are not using echo, for example, concatenating to a variable,
the best is to use variable expansion inside double quoted or heredoc
strings. Concatenating with dots is more than twice as slow.
Satyam
[Back to original message]
|