Justin, are you sure? I always understood concatenation to be slower
because memory had to be allocated separately for each string (prior to
concatenation) whereas with interpolation, the memory is already
allocated. Not trying to start a fight--I actually want to know your
interpretation/proof.
<book title="Advanced PHP Programming" author="George Schlossnagle" page="471">
When you run this under PHP 4.2.3, you get the following:
[...]
interpolated [...] 0.000016
concatenated [...] 0.000006
When you run it under PHP 4.3, you get this:
[...]
interpolated [...] 0.000007
concatenated [...] 0.000004
So although you see a significant improvement in the performance of interpolation [in PHP 4.3], it is still faster to use concatenation to build dynamic strings.</book>
This is a well-known optimization trick. George explains it very well in
his book. The gist is that there is no unique left operand when using
++$var; In $var++, the pre- and post-increment values both need to be
captured, so ++$var is faster (IIRC)
In other words: when you post-increment, the compiler needs to store the old value in memory separately from the incremented value, because the old value *may* be used after the operation. A respectable optimizer will look ahead in the code and see if the original value needs to be saved, and save accordingly. IIRC, PHP does no optimization without a separate module.
Intro to Programming Languages and Compilers class last fall. Optimization was the only part that I found was interesting, but we covered it the least (of course).
~Seth
On Oct 20, 2005, at 8:02 PM, Sean Coates wrote:
Sorry to jump in...
JP> I would prefer using ' for all strings except those with
JP> backslash expressions as variable interpolation is more expensive
JP> and less visible than concatenation.
Justin, are you sure? I always understood concatenation to be slower
because memory had to be allocated separately for each string (prior to
concatenation) whereas with interpolation, the memory is already
allocated. Not trying to start a fight--I actually want to know your
interpretation/proof.
JP> Please use ++$var instead of $var++ unless you must use a
JP> post ++. Prepending it is slightly more efficient and is less
JP> prone to error.
Very convincing. Do you have some grounds to study?
This is a well-known optimization trick. George explains it very well in
his book. The gist is that there is no unique left operand when using
++$var; In $var++, the pre- and post-increment values both need to be
captured, so ++$var is faster (IIRC)
S
--PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php