PHP String Manipulation Performance Benchmarking

Posted by Chad

Believe it or not, but I’ve been off the Rails for a couple months now. I went back to PHP because of the speed issues with Rails (and Ruby in general). As I mentioned, video.conductr was so slow on Rails I had to rewrite the whole thing in PHP. It wasn’t that bad, it’s a dead simple application. But, that’s what got me working with PHP again – and I love the speed.

Now I’ve started working on a new project that is quite large. I learned a lot about OOP from using Ruby which has made coming back to PHP tolerable being that it now has better support for objects. (I jumped ship sometime before PHP5).

Anyways, the point of writing this article is because I always refactor my code for performance. My refactoring process often includes looking for double quotes which can be single quotes, rethinking my functions, loops, and a whole bunch of string manipulations. But, as I was refactoring I start to wonder what kind of gains I was receiving or whether they were even worth my time.

To further spark my curiosity, I ran accross these tips today courtesy of DZone. So, I decided to write up a little benchmarking script to see how much difference my refactoring has on overall performance.

Benchmarking PHP String Manipulations

After refreshing the page a few times it’s easy to see that the performance advantages are so small that they are not even consistent. The typical performance gain is a few tenths of a second per 100,000 executions.

Build Slide Show Movies from Flickr with Video.Conductr

Posted by Chad

In the past I mentioned an application that I was building using Rails and the Flickr API. After building it, it turned out to be way too resource intensive for me to host with Dreamhost. Since then, I rewrote the application in PHP and it is a bit faster. It’s still a bit of a CPU hog , and Dreamhost probably isn’t too happy about that, but I wanted to put it up anyway.

The application is video.conductr. It searches Flickr for images with your search terms in their tags, text descriptions, etc. and pulls the images together into a Flash video. The videos can be viewed on site, embedded on other pages, or even downloaded as MP4. No account registration is required, however, it will help you keep track of your movies, registered users’ movies have priority in the queue, and you will get a friendly email notice when your movies have been produced. To keep down on resource use, I am currently producing about 3-6 movies per hour maximum. If the site gets heavy usage, I will put it on a better machine and this limitation will be gone.

“Urban” was a recommended tag today when I visited the Flickr homepage. Here is the video and embedded below.

TODO:
  • Fix movie introduction text
  • Fix view counter
  • Add commenting system

10^9: A One Billion Pixel Project - Beta

Posted by Chad

My latest project has just been released in Beta. It’s at 9figs.com and is a goal oriented project. The goal is to receive 1 Billion pixels, uploaded from users, to display in our widget.

This is built with Ruby on Rails except 2 files. The scripts that serve up the widget content and handle the resulting click-throughs are PHP. As the project grows, these files will likely receive a very high number of requests so RoR speed issues are a concern. Initially, I wrote them as part of the Rails app and then ported them over to PHP. Since, it has been over 2 years since I’ve touched anything ending with .php, this made the transition easy. PHP still remains extremely easy to pick up. You just have to remember to end all your lines with a semi-colon.

The widget serving file was extremely slow in Ruby. Each request generated several RMagick objects and it would not have taken many requests to pull the server to a halt. Or, require a more advanced (read: expensive) hosting solution. I was impressed by how fast the same logic could execute when written in PHP. I do miss that benefit of PHP.

I was able to build the entire app in 28 hours of development time. Speaking as a freelancer who only codes a few hours a week, productivity is key. So far, I haven’t found a case where I am not more productive in Rails than I would be in PHP (and I’m not interested in learning anything else).

Go check out the project and get the widget up on your site.