|
Posted by tg-php on 10/07/57 11:25
Wow! Fantastic rundown Satyam! Thanks for posting such a complete analysis. I had no idea that you could use commas instead of periods to join multiple strings much less do it without concatinating, that's very interesting.
I don't think I've ever seen that used in any sample code before. I'm wondering if it's common enough that it'll stay around or if it'll become deprecated at some point (or is it new to PHP5? I havn't upgraded yet). I'd hate to start using it then have to go back and fix all my code when it gets cut. (Any thoughts/info anyone?)
I feel somewhat vindicated now at my use of echo "blah $something blah" versus print 'blah ' . $something . ' blah', which is the standard used in the code I inherited. hah
Anyway, thanks again Satyam for the rundown. It's greatly appreciated.
-TG
= = = Original message = = =
<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
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.
[Back to original message]
|