Jump to content
Larry Ullman's Book Forums

Php - Coding With Speed And Efficient/Lean Code In Mind


Recommended Posts

Can somebody provide a general overview of how to code with site performance in mind (memory, CPU usage, calls to DB, etc)?  If this isnt a good conversation to post here can somebody please add some links that I can follow out and learn more?

 

For instance, from my studies so far and general knowledge:

 

1. Obviously less characters creates smaller file sizes.

 

2. Creating user defined functions eat memory, but there is some "tipping point" where they become a performance improvement vs. a burden - how do I objectively determine this?

 

3. Creating templates for Header / Footer is probably a good idea for cleaner code and ease of use, but will I gain any (even if its .5%) performance if the Header/Footer is hard coded to every page?  Wouldn't the browser cache specific components to help performance and place the burden on the user's computer rather than the server?

 

4. I read this book through before running back through again and completing the tuts (now on Ch. 4).  I remember seeing lots of tips for MySQL performance - what is the best resource for really exploring MySQL performance, and is it worth the time to tweak the MySQL DB with all the talk about the NoSQL DBs being so much faster? 

 

5. Any other ideas, sites, links for PHP and MySQL performance?

 

Thanks!

 

 

 

 

Link to comment
Share on other sites

You over think performance gains here. Don't focus on the 5% optimizations, but those that really drag your execution time down.

 

The keyword in that regard is loops. If you write a function that iterate an array once, your performance is called O(N). If execution on 1000 array Elements is 3 seconds, doubling to 2000 will execute in 6 (linear increase). If you nest another loop into your first, you'll get O(N2). If 1000 elements takes 3 seconds, 2000 will take 9. (Exponential growth) Nest even one more, and you'll have an exponential growth. If 1000 elements takes 3 seconds, 2000 will take 27...

 

Performance is also never the only consideration. You sacrifice performance for code clarity, organization, structure, time to develop, etc. If performance is the only consideration, none of us would play around with PHP.

 

My suggestion is focus on performance when you really need it. If you are really interested, read about Big-O analysis as it's clearly the most important code optimization.

 

General tip: Avoid the extra loops if you can help it. If you ever nest two or more loops, (having three or more loops in total) a red light should flash. Don't run DB queries in loops.

  • Upvote 2
Link to comment
Share on other sites

I agree with Antonio. Performance is important, but you don't want to sacrifice everything else for the sake of performance.

Unless you have a really big site that's getting hits in the millions, you probably don't need to worry too much about massive optimization.

 

Also, unless you're doing some heavy calculations in PHP (which is unusual), the time it takes for PHP scripts to be processed and maybe generate HTML markup is very small compared to loading all the images and other resources required on most pages these days.

 

Like Antonio said, the key is to go for the big gains that require a small amount of work (i.e., the low-hanging fruit), and if budget and time allow for it, go for smaller optimizations here and there.

 

Lastly, while the following emphasizes front-end more than back-end, I think the following article is an excellent start for website optimization:

http://developer.yahoo.com/performance/rules.html

Link to comment
Share on other sites

Very Cool!  Thank you both for your time in your responses.  I am more a designer and just now seriously getting into PHP so I have knowledge about front end optimization, but, you are right it might make sense to focus more on front end (image optimizing, sprites, etc - I've seen that yahoo site before and should revisit it).  I've never heard of "Big-O" analysis so I will look into that further.  Thanks again for your help! 

 

Actually, you sparked one last question regarding image optimization which is obviously important.  Lets say a site I have planned calls for users to upload photos - how do you best optimize the photos with PHP?  I have heard of and briefly read up on image processing  before but I'm not sure what the "good programmers" use when looking to efficiently upload and compress photos for faster browser retrieval/loading.  I understand PHP can do this - any tips/tricks you can share here?

Link to comment
Share on other sites

Yes, you can use the GD library built into PHP (http://php.net/manual/en/ref.image.php) to process images.

 

Quite often though, the biggest issues with image processing in PHP are security related, not image-editing related.

You have to be extremely careful when creating image-upload scripts, as you will allow your entire site, DB, etc. to be hacked if you're not careful.

I highly recommend starting with the following link to bone up on image-upload security in PHP:

http://stackoverflow.com/questions/4166762/php-image-upload-security-check-list

 

To answer your questions though, I was at a recent web development conference (that Larry was at too), and one of the best presentations was given by one of the head web optimization engineers at Etsy, and he said that the biggest optimizations for them have come through being able to better handle all of the images on their site.

Specifically, when people upload photos/images, they generally don't like it when you shrink down and compress them.

As such, they have a system that automatically turns each photo into about ten different sizes and resizes everything without any loss in quality (i.e., lossless compression).

They then use CDNs to serve up something like 90% of the image requests that are made by users.

 

Granted, that's all for handling a company that has hundreds of millions of images and millions of unique visitors a month, but all the same, optimizing image manipulation while keeping things secure is likely where you will see the biggest efficiency gains in your site.

 

Good luck!

Link to comment
Share on other sites

Guest Deleted

Hartley: I didn't realize that image uploading could be such a security nightmare. I've taken what you've shared to heart. I'll keep it in mind when/if I ever code an image uploader.

Link to comment
Share on other sites

Guest Deleted

Been sitting here thinking about this conversation. How safe/dangerous would the following be?

//user submitted URL
$url = 'http://www.somephotosite.com/someuser/somealbum/prettypicture.png';

$image = new Imagick($url);

//do something with $image
Link to comment
Share on other sites

It's not dangerous at all.

The uploading of images in and of itself is not dangerous (although malicious scripts can be injected in images).

What's dangerous is when someone uses your image upload script to upload a PHP script that hacks your whole site/DB.

Link to comment
Share on other sites

Guest Deleted

Wondering two things:

 

#1 If my code above linked to an image with a bad script in it, would that be taken care of when it was made into an Imagick object? Or would I need to do something additional to prevent that?

 

#2 If that link was a link to a PHP script, and not an image, would Imagick catch that?

Link to comment
Share on other sites

 Share

×
×
  • Create New...