SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    implode faster than concat?

    For instances where you need to append several items to create a single string, I have heard is is faster to do this
    PHP Code:

    echo implode(" ", array('string1'$var1'string2'$var2) ) 
    than this

    PHP Code:

    echo 'string1' $var1 " " 'string2' $var2
    Yes, I know I can embed variable in a string in double quotes and all that, but I'm asking about instances where that will not work, like when you are appending strings and array values to create a single string.

    Does anyone know if there is any truth to the implode being faster than using the concatenate operator?

    B

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I personally find printf and sprintf much neater. For instance:

    PHP Code:
    printf('string1 %s string2 %s'$var1$var2); 
    Increased readibility and maintainability means a lot more to me than the performance difference between concat and implode. Have you read Tom's blog on php speed optimizations?

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wisecounselor

    Does anyone know if there is any truth to the implode being faster than using the concatenate operator?
    All string operations are extremely fast and tiny speed differences among them do not matter.

    Imagine that php spends, say, 10 processor cycles for concatenation and 20 for implode(). However, one single sql query costs 100.000 cycles, what makes the difference between 10 and 20 not that important.

    For the sake of "readibility and maintainability" I'd recommend variable interpolation:

    PHP Code:
    echo "string1 $var1 string2 $var2"


Bookmarks

Posting Permissions

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