LucosidE Posted November 26, 2009 Share Posted November 26, 2009 I am using echo to display a table with data from mysql database. Since there is quite a bit of data I want it to display as fast as possible. What is the faster way to echo elements of the row i get from mysql query result? Right now i am doing echo "blah {$row['foo']} blah"; before i had echo "blah".$row['foo']."blah"; but using the . seems slow the other options are: 1: $element = $row['foo']; echo "blah $element blah"; 2: Use a stringBuilder library found here: http://cfgmgr.mirrors.phpclasses.org/browse/file/23722.html it uses sprintf to concat strings on lower level All suggestions are appreciated, Thank you in advance Luco Quote Link to comment https://forums.phpfreaks.com/topic/183039-faster-way-to-echo-arrayfoo-or-arrayfoo-or-foo-arrayfoo/ Share on other sites More sharing options...
LooieENG Posted November 26, 2009 Share Posted November 26, 2009 I'd do it using single quotes and concatenation 'blah ' . $row['foo'] . ' blah'; If you must use double quotes, I'd use echo "blah {$row['foo']} blah"; Quote Link to comment https://forums.phpfreaks.com/topic/183039-faster-way-to-echo-arrayfoo-or-arrayfoo-or-foo-arrayfoo/#findComment-966017 Share on other sites More sharing options...
JonnoTheDev Posted November 26, 2009 Share Posted November 26, 2009 I cannot believe that the concatonation operator (.) is any different. It is the amount of data you are querying and looping through that will make things slow. If it is for on-screen display then why not paginate the results? Quote Link to comment https://forums.phpfreaks.com/topic/183039-faster-way-to-echo-arrayfoo-or-arrayfoo-or-foo-arrayfoo/#findComment-966018 Share on other sites More sharing options...
PFMaBiSmAd Posted November 26, 2009 Share Posted November 26, 2009 Benchmarks using loops of 10,000 echo statements have shown that putting a variable inside of a double-quoted string is slightly faster than using concatenation. If my memory is correct, a few 1/100 of a second for a loop of 10K. You would need to benchmark if that or a sprintf() is faster. Assigning one variable to another just to put the second variable into a string, when the first variable could be put into the string directly, would always be less efficient than just putting the original variable into the string. Quote Link to comment https://forums.phpfreaks.com/topic/183039-faster-way-to-echo-arrayfoo-or-arrayfoo-or-foo-arrayfoo/#findComment-966020 Share on other sites More sharing options...
salathe Posted November 26, 2009 Share Posted November 26, 2009 The differences in time spent using any of those methods will be negligible when compared to the time spent by your script talking to MySQL and likely other things happening in your script. Don't worry about which way of echoing a value is quicker until you really need it. Even then, do your own case-specific thorough research to arrive at a conclusion (if you can). Quote Link to comment https://forums.phpfreaks.com/topic/183039-faster-way-to-echo-arrayfoo-or-arrayfoo-or-foo-arrayfoo/#findComment-966164 Share on other sites More sharing options...
Andy-H Posted November 26, 2009 Share Posted November 26, 2009 Correct me if I'm wrong, but using a loop would mean the processor would be calculating the same logic over and over again (unless of course the variable used was created dynamically inside the loop?? which would cause in-accurate results...) Wouldn't that cause an increase in processing speed after the first iteration due to the cache controller pulling the calculated data and result into L1 Cache and causing in-accurate results for the remaining iterations? Quote Link to comment https://forums.phpfreaks.com/topic/183039-faster-way-to-echo-arrayfoo-or-arrayfoo-or-foo-arrayfoo/#findComment-966173 Share on other sites More sharing options...
MadTechie Posted November 26, 2009 Share Posted November 26, 2009 Quick benchmark (on my PC) echo "test $r"; //0.58999300003052 echo 'test '.$r; //0.60724496841431 echo "test {$r}"; //0.65190696716309 sprintf( "test %s",$r); //0.60445594787598 sprintf( 'test %s',$r); //0.52453398704529 EDIT: sample code <?php $rnd = array(); for($n=0;$n<1000;$n++){ $rnd['test'.$n] = md5(uniqid(true)); } $start = microtime(true); foreach($rnd as $K => $r){ echo "test $r"; } echo "<BR />\n"; echo microtime(true)-$start; Quote Link to comment https://forums.phpfreaks.com/topic/183039-faster-way-to-echo-arrayfoo-or-arrayfoo-or-foo-arrayfoo/#findComment-966188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.