Re: Concatenation vs. Interpolation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You're right. There is a low benefit in performance. But I think it's
valuable
in high-traffic websites. Of course, we should consider a lot of thing to
have a better performance, but Concatenation, IMO, is the best practice.
Let me explain:

َActually, the programmers' preference on readability and write-ability
varies.
I think that concatenation provides a more readable syntax, since it
allows us to distinguish variables from plain strings very easily.
Consider the following code:

$str = 'My name is ' . $name . ' and I live in ' . $city;
VS.
$str = "My name is $name and I live in $city";

(You may view the above code in an Editor with syntax-highlighting
capability for a better result). However, it is possible to offer
opposite examples.

In addition to the Performance on Speed, Concatenation uses less memory
than Interpolation, as explained in PHP Manual. Take a look at the
following URL:

http://us2.php.net/manual/en/language.types.string.php

Quote:
Note:  Parsing variables within strings uses more memory than string
concatenation. When writing a PHP script in which memory usage is a concern,
consider using the concatenation operator (.) rather than variable parsing

Thus, I think that it is better to use Concatenation instead of
Interpolation.
Since it provides Performance, both on Speed and Memory Usage.

There is a benchmark available here:
http://us2.php.net/manual/en/function.print.php#66392


On 8/11/07, Tijnema <tijnema@xxxxxxxxx> wrote:
>
> On 8/11/07, AmirBehzad Eslami <behzad.eslami@xxxxxxxxx> wrote:
> > Hi,
> >
> > FASTER CODE!
> >
> > This question involves Coding Style, Readability, Write-ability and
> > Performance.
> > Some people use the . operator to concat an string to a variable, some
> > people use interpolation (embeding variable into string).
> >
> > =====================
> > 1st Method (Concatenation)
> > $str = 'My name is ' . $name;
> >
> > 2nd Method (Interpolation)
> > $str = "My name is $name";
> > =====================
> >
> > I know that the 1st Method is much faster, according to
> > some benchmarks, which one of them is available
> > in the book "Advanced PHP Programming, page 470".
> >
> > Could we consider the 1st Method as a "Best Practice", which offers
> > better Performance?
> >
> > I know that the performance here is not very much, but is it
> > considerable in a high-traffic website?
> >
> > Thank you in advanced for sharing your opinions,
> > Behzad
> >
>
> Code readability is quite important when working with a team on a
> project, and if you got for method 1 on things like this:
> $str = '<a href="'.$url."'>';
> It gets quite inreadable, having a double qoute next to a single quote.
> while this looks much better:
> $str = "<a href='$url'>";
>
> I just did some benchmarks myself, and you can see that concatenation
> is definitly a little bit fast, but it's only 2 seconds on 10 million
> loops:
> For 10,000,000 loops:
> Concatenation single quote:5.62786102295
> Concatenation double quote:5.63359999657
> Interpolation:7.32201290131
>
> Test code used:
> <?php
>
> $x = 0;
> $str = '';
> $add = '?';
>
> $time[1] = microtime(TRUE);
> for($x = 0; $x < 10000000; $x++) {
>         $str = '<a href="'.$add.'">';
> }
> $time[2] = microtime(TRUE);
>
> $x = 0;
> $str = '';
> $add = '?';
>
> $time[3] = microtime(TRUE);
> for($x = 0; $x < 10000000; $x++) {
>         $str = "<a href='".$add."'>";
> }
> $time[4] = microtime(TRUE);
>
> $x = 0;
> $str = '';
> $add = 'def';
>
> $time[5] = microtime(TRUE);
> for($x = 0; $x < 10000000; $x++) {
>         $str = "<a href='$add'>";
> }
> $time[6] = microtime(TRUE);
>
> echo 'For 10,000,000 loops:';
> echo '<br />Concatenation single quote:'.($time[2]-$time[1]);
> echo '<br />Concatenation double quote:'.($time[4]-$time[3]);
> echo '<br />Interpolation:'.($time[6]-$time[5]);
>
> ?>
>
> Tijnema
>
> --
> Vote for PHP Color Coding in Gmail! -> http://gpcc.tijnema.info
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux