Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    380
    Thanks
    36
    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?
    Code:
    $var = "$var in here";
    Also, is sprintf() also as slow as printf()?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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");
    print(
    $out);
    // 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.

    Edit:
    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 10:08 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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