homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
R. v. Concatenation of Strings, ex p. Pushing/Joining of Arrays
Some thoughts on how to join strings.
andreasfriedrich




msg:1261059
 12:12 am on Oct 15, 2002 (gmt 0)

In Perl the standard way to build up a string from many different sources is to push every element into an array and join it later on.

When I began to code in PHP I saw a lot of examples using the concatenation operators "." or ".=". Being used to the array approach I tended to use that one in PHP as well. However, I have always wondered whether this was the way to go in PHP too.

Today I tested it finally.

Code

<?php 
/*
Comparing speed of concatenating strings
and pushing and joining arrays
*/
#
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
#
$x = array('Taylor','Zac','Isaac','Leo','Aaron','Nick','Scott',
'Dave','Bob','Clint','Chris','Thomas','Ryan','Dennis',
'Thimo','Steve');
#
$s1 = microtime();
foreach($x as $val) {
$s .= $val . ', ';
}
echo $s, '<br>';
$e1 = microtime();
#
unset($s);
$s = array();
#
$s2 = microtime();
foreach($x as $val) {
array_push($s, $val);
}
$y = implode(', ', $s);
echo $y, '<br>';
$e2 = microtime();
#
$t1 = (getmicrotime($e1) - getmicrotime($s1));
$t2 = (getmicrotime($e2) - getmicrotime($s2));
$td = $t1 - $t2;
#
echo "using concat: $t1 seconds<br>
using array: $t2 seconds<br><br>
difference: $td seconds";
?>

Output

Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Dave, Steve,
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Dave, Steve
using concat: 0.000538945198059 seconds
using array: 0.00032103061676 seconds

difference: 0.000217914581299 seconds

Conclusion

Donīt use the concatenation operator. Pushing the string parts into an array and then joining the array is about 40% faster. Additionally in a lot of cases of makes your job way easier if you want to add some glue string between the elements but not after the last one.

Andreas

 

jeremy goodrich




msg:1261060
 10:41 pm on Oct 15, 2002 (gmt 0)

So pushing is faster in PHP than concatenation, do you know which is faster in perl? :)

(Thanks for sharing the results of your benchmarking...given the number of threads recently about which language, or what's faster, it's very helpful to see it all laid out and tested.)

Cheers.

andreasfriedrich




msg:1261061
 10:43 pm on Oct 15, 2002 (gmt 0)

Pushing as well :)

And to quote from another thread [webmasterworld.com]:

And I thought no one cared... ;)

dingman




msg:1261062
 11:05 pm on Oct 15, 2002 (gmt 0)

Cared, made a mental note, just didn't have much to say about it :)

lorax




msg:1261063
 12:10 am on Oct 16, 2002 (gmt 0)

Hey! That's my quote! :)

Thanks for the tip Andreas! Tis always nice to learn something that could come in handy. I haven't had a chance to look your code over carefully as I have one question regarding outputs but I will tomorrow.

GB

jatar_k




msg:1261064
 1:34 am on Oct 16, 2002 (gmt 0)

yep, I cared too. ;)

good stuff andreas

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved