Welcome to WebmasterWorld Guest from 54.159.242.217

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

R. v. Concatenation of Strings, ex p. Pushing/Joining of Arrays

Some thoughts on how to join strings.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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

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

Senior Member

WebmasterWorld Senior Member jeremy_goodrich is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 4, 2000
posts:3468
votes: 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.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


Pushing as well :)

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

And I thought no one cared... ;)
11:05 pm on Oct 15, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 12, 2002
posts:885
votes: 0


Cared, made a mental note, just didn't have much to say about it :)
12:10 am on Oct 16, 2002 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 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

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


yep, I cared too. ;)

good stuff andreas

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members