Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Speed or Readability?

I noticed in some of your PHP videos you are appending the data to a variable and then outputting... Is there any value in doing it the short way opposed to the way you have done it?

e.g

$output = "";

foreach($array as $key => $val){
    $output .= 'Append me to output';
}
echo $output;

Instead of:

$output = "";

foreach($array as $key => $val){
    $output = $output . 'Append me to output';
}
echo $output;

Are there any speed implications of doing it either way?

Thanks for your time. Jon

9 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

There shouldn't be any speed implications of doing it either way. I wanted to keep the code simple and straight-forward for the beginner version of the project, but I'll definitely be introducing techniques short-hand techniques like <code>.=</code> as we continue on to the intermediate and advanced version of the site.

Thanks for the reply Randy... Perhaps you could answer 1 other thing for me?

With a rather large mysql query which returns around 200 results (which I need to output) is it best to put those results into an array (output that), free the result and then start on the next query? Instead of outputing directly from the fetched results?

I only ask as I have a site which is very slow to load which is currently echo'ing each row from the result in a while loop.

Thanks for your time.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Looping through the fetched results is technically the best way for performance. Loading the results into an array and then looping through that array separately is a little slower and a little more memory intensive.

Even still, I usually do load the results into an array like you suggested because it is easier to read and debug later. Unless you are dealing with a very large result set, then you probably won't notice a difference. For a result set with only 200 results, I wouldn't think either option would matter.

Steve Monsen
Steve Monsen
5,207 Points

And while it's not a good idea to rely on caching to compensate for inefficient code, it's useful to remember that some of the performance drag that can't be refactored away may likely be cached in some way in a production environment through:

  • Asset caching (html, css, js)
  • Object caching
  • DB Query caching
  • Memory caching of various sorts
  • etc...

Again, it's never a good idea to compensate for bad code by attempting to paper over that fact with caching and performance tuning techniques. But it's good to maintain perspective so you don't get too overly critical (and waste time on) refactoring code that is likely to be cached regularly anyway.

It's a bit of a balancing act, but you get a feel for it as you gain experience.

James Barnett
James Barnett
39,199 Points

My view on speed vs readability is:

"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity." -- W.A. Wulf

Here's a great article on the downsides of PHP micro-optimization about why it's not a good idea to trade optimization for readability.

Well thank you all, I will look into the best way of optimizing the queries somewhat, I know the basics like not using SELECT * etc but perhaps need to look into other stuff as well (I guess there are other things to try).

@Steve, my site is cached by cloudflare, however I believe that to be only the images / css / js (which are also minifified on the fly). I dont have a clue about database caching, and probably need to start learning more about the database stuff.

Again, thank you all for your comments. Jon

Steve Monsen
Steve Monsen
5,207 Points

@Jonathan Rawlins

Yes, you're right, CF doesn't cache HTML pages by default, but they do have the ability to do so if necessary.

https://support.cloudflare.com/entries/22036831-how-do-i-cache-static-html

Also, if you're using Wordpress, there is a plugin called W3 Total Cache that has caching and minification options for just about everything you could imagine. Even if you don't use the plugin it would probably be worth it to see what it caches and read the help files, etc.

Thanks for the heads up Steve Monsen, I will have a look into it... Although not sure in my case caching will be ok, as this is some custom built scripting which displays players laptimes / qualifying times (which need to update when you refresh the page).

I will see what options I have as to looking into caching it manually in my code (not done anything with caching before).

Thanks again for the help and info.

Here is an example of the slow loading page: http://www.mxscentral.com/tournaments/results/4/rnd-1/qualifying