Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I wrote a PHP application (kind of a journey planner) a few months ago that at "beta" took about 1 minute to run each time due to all the processing it did. I spent a good few days speeding it up and making it use less memory and it now runs in under 10 seconds. Here's a few things I did that worked for me.

1. Install APC and use it to cache objects as well as PHP files. Cache wherever possible. I used APC lots, as well as caching some heavy processing to disk. Cache results of expensive functions that are called many times in 1 script in a global or static variable.

2. Unwrap lots of the lovely OO wrappers, such as the ORM (similar to ActiveRecord). This made the code messier but much faster. PHP takes a big hit every time it instantiates a new object.

3. Take advantage of PHP's copy-on-write memory allocation; understand how PHP does garbage collection; use references where possible; understand what PHP is doing behind the scenes.

4. Profile with xdebug and kcachegrind. Great for finding what's taking up the time and which functions are being called many times. Inline small functions that are called many times.



Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: