Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Thanked 25 Times in 25 Posts

    Variable parsing in strings

    For: http://mgccl.com/php-speed-freaks
    S/he said:
    echo "$variable string";
    is slower than
    echo $variable.'string';
    Does the same thing apply for this?
    $var = "$var in here";
    Also, is sprintf() also as slow as printf()?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    Using double quotations is always slower than using single quotations. This is because the PHP interpreter considers that double quotations can contain parsable data while single quotations can be only string data.
    PHP Code:
    echo "$var is awesome";
    // Is slower than
    echo $var ' is awesome'
    My understanding of printf is that it is slower than sprintf. Printf is likely chained to the sprintf command and include both sprintf and print for you. I'd have to run test to be certain, but I'd suspect that:
    PHP Code:
    $out sprintf("%s""Hello World");
    // Is approx the same time as
    printf("%s""Hello World"); 
    I'd have to do test to be certain, but I would suspect this is the case.

    I decided to reword this :P
    printf beats sprintf in more than 65% of my tests. Therefore, running a printf is faster than a sprintf especially if its followed by a print.
    Last edited by Fou-Lu; 07-14-2008 at 11:08 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts