By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,743 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,743 IT Pros & Developers. It's quick & easy.

tuning php: variables inside strings

P: n/a
(N.B. - this may be a well-known thing, but it was new to me). I was
reading a script written by someone else, and noticed a coding style
difference - they always concatenated variables into strings, while I
always keep them inline. Out of curiosity, I wrote the following (with
some help from the online php man page on microtime()) to see which was
more efficient. Not only is my way (in-line) worse, it's *much* worse.

If anyone has a different interpretation, let me know.
<?php
/* Values from our dev server:
Concat string (50,000 reps):
1.156
1.127
1.128

In-line string (50,000 reps):
3.524
3.312
3.287

*/

function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}

$val1 = 1;
$val2 = "Hello World";
$start = microtime();
for ($i = 0; $i < 50000; $i++) {
$string1 = "First part of string ".$val1." Second part of
string ".$val2." final part";
// $string2 = "First part of string $val1 Second part of string
$val2 final part";
}
$diff = microtime_diff($start, microtime());
$duration = sprintf("%0.3f", $diff);
echo "Processing took $duration seconds";
?>
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Greg Bryant wrote:
(N.B. - this may be a well-known thing, but it was new to me).**I*was
reading a script written by someone else, and noticed a coding style
difference - they always concatenated variables into strings, while I
always keep them inline.**Out*of*curiosity,*I*wrote*the*following*( with
some help from the online php man page on microtime()) to see which was
more efficient.**Not*only*is*my*way*(in-line)*worse,*it's*much*worse.


<snip>

You'll also find it's faster if you use single quotes instead of double
quotes, as there's no requirement for variable interpolation with a single
string.

eg use

$foo = 'bar1'.$foo1.'bar2';

rather than

$foo = "bar1".$foo1."bar2";

A lot of this coding style comes down to personal preference, as some people
feel code can look messy and be harder to maintain when concatenating
strings.

From your tests it appears that inline is 3x slower than concatenation but
you're looking a such a small amount of time (1sec for 50k reps and 3sec
for 50k reps) that only mere fractions of a second separate the two when
only doing it once. Obviously if your application will experience huge
numbers of visitors you want to keep it as quick as possible though.

Chris

Chris Hope
The Electric Toolbox Ltd
http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
Chris Hope <bl*******@electrictoolbox.com> wrote in
news:Dj******************@news.xtra.co.nz:
From your tests it appears that inline is 3x slower than concatenation
but you're looking a such a small amount of time (1sec for 50k reps
and 3sec for 50k reps) that only mere fractions of a second separate
the two when only doing it once. Obviously if your application will
experience huge numbers of visitors you want to keep it as quick as
possible though.


My first set of tests was at 500,000 instead of 50,000, and the test seems
to scale - the concat was at 11+ secs, and the inline was over 30 (past the
execution limit on our dev server :-).

Thanks for the tip on the single quotes - I'll play with that, too.

Greg
Jul 17 '05 #3

P: n/a

Uzytkownik "Greg Bryant" <br**********@yahoo.com> napisal w wiadomosci
news:Xn**********************************@199.45.4 9.11...
(N.B. - this may be a well-known thing, but it was new to me). I was
reading a script written by someone else, and noticed a coding style
difference - they always concatenated variables into strings, while I
always keep them inline. Out of curiosity, I wrote the following (with
some help from the online php man page on microtime()) to see which was
more efficient. Not only is my way (in-line) worse, it's *much* worse.

If anyone has a different interpretation, let me know.


Well, you test doesn't really replicate a real-world situation though.
Through every loop the length of the string remains the same, and the string
is freed at the end of the loop so that the same memory block can be reused.

I think in actual use, variable interpolation would be faster than multiple
concatenations, since few memory allocation is involved. For "First part of
string $val1 Second part of string $val2 final part", PHP only needs to
allocate memory once, whereas for 'First part of string '.$val1.' Second
part of string '.$val2.' final part', it has to allocate memory for each of
the concat operation (four in all). This overhead doesn't show up in your
test because there're always that many blocks in the memory cache of
precisely the right size.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.