the joy of eliminating code that sucks...

"the joy of eliminating code that sucks is so great i must tell you all about it right now in case the world ends this very minute"

Working on my website PHP code off-line out of the wild. It's code which has existed for a few years now. Some of the routines might hark back to when I first started coding in PHP in 2005(?).

At some point between now and then I created a keywords facility. The first implementation was really slow. The second implementation was a vast improvement in that it didn't collate and order all the keywords twice.

As my skills in PHP advanced I wanted it to do more and consequently added bits and pieces here and there. One of thoses bits and pieces was the ability to link to the parent of a page, a random page, or the current page. Having once coded in C++, the way of linking to the current page was to link to *this, and consequently linking to a random page became *random. The easy way of doing this to get it to work aseasily as possible was to blindly use str_replace on *every single link* that was created.

The keywords page - without any keyword refinement - currently produces 717 links. That's over 2100 calls to str_replace. In Debian it doesn't seem to be an issue, but on Arch, generating the page took 8 seconds.

After a few false starts micro-optimizing link and keyword code to no avail, I saw the str_replace calls and wondered what would happen if I made them conditional on strpos finding an asterisk in the link data.

Well what do you know? That simple test reduces the keyword page build time down from 8 seconds to just under half a second. Lah-dee lah-deedah!


"the joy of eliminating code that sucks..."

Short text about optimizing PHP code for my website

Journal entry - 06:28 Saturday 12 November 2011

DISCLAIMER: The opinions and attitudes of James W. Morris as expressed here in the past may or may not accurately reflect the opinions and attitudes of James W. Morris at present, moreover, they may never have.


this page last updated:29th April 2013 (C) 2003 - 2017 James W. Morris

script time:0.0420