Should Readability suffer for Performance?

Being a PHP developer, I often spend my spare time at work by browsing through blogs and articles on good coding practices, performance optimizations, tips, tutorials and so on. There have been some very resourceful and informative writings on these topics which helped me a lot. However there are some optimization tips on these articles that I found to be trivial, which may gain you a few nanoseconds of faster code at the expense of poor readability. For example, this article introduces a faster approach for detecting the length of a string:

if (strlen($foo) < 5) { echo "Foo is too short"; }

vs

if (!isset($foo{5})) { echo "Foo is too short"; }

The second approach, according to the article, is faster. May be, but should I quit using strlen() from now on? As far as I know, in a professional environment, the quality of code is not only measured by how fast it runs(performance) but also how easy it is to comprehend(readability) by someone other than the coder. It is more convenient to use strlen() to check the string length, because even if someone didn’t know the method, he/she could have guessed what it does from its name. Why obfuscate the code for a performance improvement that is barely comprehensible?

Another example of this, pushing an item in an array:

array_push($stack, $var) vs $stack[] = $var;

The second approach is claimed to be faster. I’ll still use the first one though. Its clear and convenient, I don’t mind if its a bit ‘slow’.

Finally there is an ongoing debate on the use of __autoload(). It clearly is better than require/include/require_once because includes the scripts at runtime, making the code cleaner and more manageble. However it has been said autoload() and other magic methods decrease performance and their use should be avoided. Personally I would prefer to use autoload() since I code for maintainability. Performance can be optimized at the hardware level.

Update: The SPL autoload functions can be used instead of __autoload() to improve performance.

Advertisements

2 thoughts on “Should Readability suffer for Performance?

  1. I tend to make readability paramount.Computers will inevitably get faster, and nanoseconds will go unnoticed. Unless you’re doing assembly, there’s no reason to pre-optimize other than to try and make yourself look smart. Eventually, people will catch on and when they use your code, they will hate it, and it will get thrown away. Bottom line.

    “Good programmers write code that humans can understand.” — Martin Fowler

  2. Your post remind me of an article I read. It was an interview with a guy who is responsible for the Java virtual machine. From what I remember, the interview talked performance improvement of string concatenation with operator+ in Java 6.

    At the end of the interview, the VM guy suggests java developers to write the most obvious code and avoid performance hacks, so that the VM can optimize for you.

    I know, PHP is not JAVA, but if strlen() is really that slow in PHP, I’m sure the PHP guys will figure it out and optimize it. Who knows, maybe PHP6 or 7 or 8 will see you’re using strlen() in an if, and optimize it for you!

    I am someone who prefers code readability over performance hacks any day.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s