Re: [PHP] Concatenation vs. Interpolation

2007-08-13 Thread Richard Lynch
On Sat, August 11, 2007 2:21 am, AmirBehzad Eslami wrote:
 I know that the performance here is not very much, but is it
 considerable in a high-traffic website?

It's almost for sure meaningless in any real-world website.

Use valgrind callgrind to find your real bottlenecks, and focus on
those instead of wasting your time optimizing meaningless snippets of
code.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Concatenation vs. Interpolation

2007-08-12 Thread Danial Rahmanzadeh
In my opinion, concatenation reduces code readability because unlike
java(that uses + operator), php uses dot(.) for concatenation consider the
following codes:
//java
String mystr=hello+world;
is more readable from:
//php
$mystr=hello.world;

if you only need echoing something to output(which is not usually the case
in MVC and/or other effective methods), the fastest way is using comma:
echo 'hello',$mystr,blah,blah ;


[PHP] Concatenation vs. Interpolation

2007-08-11 Thread AmirBehzad Eslami
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


Re: [PHP] Concatenation vs. Interpolation

2007-08-11 Thread Tijnema
On 8/11/07, AmirBehzad Eslami [EMAIL PROTECTED] 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.6335657
Interpolation:7.32201290131

Test code used:
?php

$x = 0;
$str = '';
$add = '?';

$time[1] = microtime(TRUE);
for($x = 0; $x  1000; $x++) {
$str = 'a href='.$add.'';
}
$time[2] = microtime(TRUE);

$x = 0;
$str = '';
$add = '?';

$time[3] = microtime(TRUE);
for($x = 0; $x  1000; $x++) {
$str = a href='.$add.';
}
$time[4] = microtime(TRUE);

$x = 0;
$str = '';
$add = 'def';

$time[5] = microtime(TRUE);
for($x = 0; $x  1000; $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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Concatenation vs. Interpolation

2007-08-11 Thread AmirBehzad Eslami
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 [EMAIL PROTECTED] wrote:

 On 8/11/07, AmirBehzad Eslami [EMAIL PROTECTED] 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.6335657
 Interpolation:7.32201290131

 Test code used:
 ?php

 $x = 0;
 $str = '';
 $add = '?';

 $time[1] = microtime(TRUE);
 for($x = 0; $x  1000; $x++) {
 $str = 'a href='.$add.'';
 }
 $time[2] = microtime(TRUE);

 $x = 0;
 $str = '';
 $add = '?';

 $time[3] = microtime(TRUE);
 for($x = 0; $x  1000; $x++) {
 $str = a href='.$add.';
 }
 $time[4] = microtime(TRUE);

 $x = 0;
 $str = '';
 $add = 'def';

 $time[5] = microtime(TRUE);
 for($x = 0; $x  1000; $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