Welcome to WebmasterWorld Guest from 54.147.44.93

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.

     

andreasfriedrich

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

WebmasterWorld Senior Member 10+ Year Member



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

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

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



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

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

WebmasterWorld Senior Member 10+ Year Member



Pushing as well :)

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

And I thought no one cared... ;)

dingman

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

WebmasterWorld Senior Member 10+ Year Member



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

lorax

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

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



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

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

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



yep, I cared too. ;)

good stuff andreas

 

Featured Threads

Hot Threads This Week

Hot Threads This Month