ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Parsed variables are faster than Concatenated variables

April 15, 2012 12:17am

Subscribe [2]
  • #1 / Apr 15, 2012 12:17am

    coolfactor

    354 posts

    Throughout CodeIgniter’s code is the use of concatenation.

    A simple example:

    include_once(APPPATH.'controllers/'.$class.'.php');

    With PHP, using concatenation for $variables is a convention rather than a rule. PHP has another way to incorporate the value of a variable into a string… parsing.

    The same code can be written as this:

    include_once(APPPATH."controllers/{$class}.php");

    Now, you might think that parsing would actually take longer to execute, but it turns out that it’s faster than concatenation and has the nice side effect of being easier to read in most cases.

    Note: the performance improvement may be negligible in most cases, but every little bit helps when thousands of concatenations may be done for each request to the framework.

    You can even call object methods within a parsed string:

    $str = "My name is {$me->getName()}."

    I would personally like to see the CI developers adopt the parsing approach.

  • #2 / Apr 15, 2012 12:40am

    skunkbad

    1326 posts

    I’d think that something like this would only be beneficial if you had a super huge high traffic website. It would be interesting if you did some benchmarking and showed some numbers to us.

  • #3 / Apr 15, 2012 5:22am

    wiredesignz

    2882 posts

    Interpolation is about 40% slower than Concatenation. Do some research.

    http://www.google.com/?q=php+concatenation+vs+interpolation

  • #4 / Apr 15, 2012 6:00pm

    Phil Sturgeon

    2889 posts

    Here is an interesting article on the subject:

    http://nikic.github.com/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth

    But as the article says, the whole conversation is extremely pointless.

  • #5 / Apr 16, 2012 2:00am

    wiredesignz

    2882 posts

    @Phil Sturgeon,
    While the article you linked to, might suggest that parsing large double quoted strings may be no worse or even better than concatenating many separate variables, it needs to be stressed that double quoting purely text strings, without embedded variables, will create an unnecessary performance hit that should be avoided. And even if the performance hit is negligible it is better that a developer is aware of the facts and able to make an informed decision about such things themselves.

  • #6 / Apr 18, 2012 3:20pm

    Phil Sturgeon

    2889 posts

    Absolutely. If there is only going to be a string and nothing else, then singles are a bazillionth quicker than doubles. If you are then adding variables to the string then singles are quicker for one variable, and doubles are quicker when handling more - at least that’s what this article suggests. It’s an old wives tale that “singles are always quicker than doubles”, which has been floating around the PHP community for a while.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases