Matt Clark

When to Optimize Code for Performance

May 4th, 2009 by Matt Clark in Web Development

Question:  When should a web developer optimize?

Answer:  When you have to improve the performance.

Of course writing bad or inefficient code is not a good way to develop code.  But spending time optimizing code before you know there is a problem can be a waste of time.  I value legible and readable code over unnecessarily-optimized code anyday.

If a web page or certain feature is too slow, then diagnosing exactly where to optimize is appropriate.  Here are some things to consider and test:

Is the bottleneck in the code or within a database call?
Perhaps the database needs to be indexed or normalized.
How many SQL calls are being performed?
Caching the database calls may solve this issue.
Is a database the best solution for storing all your information?
LDAP or memcache are two other options that may work best depending on what you are trying to do.
Are your files and images just too big?
Be sure the images are appropriately compressed, and that server-side compression for text files (HTML, CSS, JavaScript, XML) is activated.
Could any of the heavy logic code be pre-computed?
I recall one project (Straight Line Performance Solutions) where I pre-computed a big table of possible values from a complicated statistical function, and just looked up an approximate number quickly instead of spending 5-10 seconds computing the exact value.
Are you using a bloated CMS (content management system)?
If you are and the web pages are static, then a page-caching system will work wonders.
Is the server limited by bandwidth, RAM, or CPU?
Monitor server performance indicators to see if  there is too much disk thrashing.  Hardware is relatively cheap nowadays, and may be a better solution than spending days unsuccessfully tweaking code.
Does the page feel like it loads fast enough?
Perception is everything to a web user.  Some non-essential web page items could be changed to load a few seconds after the visible part of the web page is loaded and refreshed.  For example, there’s no reason to preload all images in a Javascript-based slide show.
Is there just too much traffic to your site?
Wow – what a great problem to have!  Maybe it’s time to scale your web application to multiple servers or host your application in the cloud.  There are sometimes session-management issues when using multiple web servers, but some real smart folks have figured most of this out for the major languages and popular frameworks.

There are only some ideas of what to look for – and I’m sure I missed a bunch of simple ones.  As you can see, I recommend spending time diagnosing the real problem before jumping in and optimizing code right away.

I’d rather have a correct program than a fast incorrect one.  Optimizing code when needed is better than having to debug faulty code.

Tags: , ,

Matt Clark worked in academia, corporate research labs and several technology startup companies prior to GORGES. His expertise is software architecture, database development, and system administration. Matt brings GORGES over 25 years experience developing fast and robust software on a multitude of platforms and languages.

Leave a Reply