Grouping PHP classes = faster by 24%

February 12, 2006

Out of sheer boredom I wanted to see which was better, including one big file of classes or splitting your classes up into multiple files. I'm going to use nusoap as an example. The NuSoap package lets you download one file. nusoap.php. That one file contains 9 classes. While not only is this method nice as you only need to include one class, its also 24% faster on average than having to include 9 seperate files.

I broke the nusoap.php class out into 9 seperate files, one for each class as most people organize their libraries. I then included the 1 nusoap file, then recorded the time, then included the 9 nusoap files and recorded the time. After 50 iterations the average was that including multiple files was on 24% slower than the one large file.

So what's the lesson? If you're distributing your PHP software package and/or push your scripts to a live server, think about combining your classes by directory into one big file. Lets say you have a directory like this

If all of those files have to be included to create a user object, just create a master user.php file with all of your classes built into that file. The upside is a little faster performance and easier/less includes. The downside is it might be slightly harder to maintain. The trade off is decided by your traffic load. Some of your out there like to eek every little nano second you can out of your scripts. :)


RSS feed for comments on this post.

  1. Eddie Peloke says:
    February 12, 2006 @ 18:39 — Reply

    Slow weekend Jim? :)

  2. Jim says:
    February 12, 2006 @ 19:29 — Reply

    so slow Eddie ;) I've been buried in a fowler book and needed a break.

  3. Dennis Pallett says:
    February 13, 2006 @ 06:21 — Reply

    What you could always do is during development have seperate files, and then use a checkout / build script which automatically adds similar classes to one file. Best of both worlds.

  4. Guest says:
    February 13, 2006 @ 06:29 — Reply

    If you use some opcode cache the 24% difference will be gone.

  5. Nate K says:
    February 13, 2006 @ 06:55 — Reply

    Interesting results! I can honestly say I have not tested something like this. The only problem I would see is the case where not ALL of those classes would need to be used to create a specific object. You would, however, still be loading a single file of all of the classes, some of which may never get used (maybe this really doesn't take a toll on the performance - im not sure). Good read, especially since I am looking into some caching options right now with my server. Peace, nate

  6. Richard Davey says:
    February 13, 2006 @ 15:20 — Reply

    Not wanting to knock your findings in any way - but surely it is bound to be faster simply because there are 8 less file operations happening? Hard drives take time to 'do stuff', so anything that reduces this is bound to speed up the initial preparation of the scripts for php to then parse. Any cache that worked on a file level would negate this improvement I'd imagine. More importantly though, if you didn't need all 9 classes for the specific request that was made, that is memory overhead you could do without (granted in this example you did need them though!)

  7. Buddha443556 says:
    February 17, 2006 @ 08:10 — Reply

    How about some numbers, showing your results, to go with that 24% increase? After all, 24% of nothing is nothing.

  8. Jim Plush says:
    February 17, 2006 @ 13:26 — Reply

    buddha, numbers can vary on system to system, whether you're doing remote file includes, server farm includes, different operating systems. I tested on several platforms and the average of all was 24% some were .0009 seconds, some were .1000 seconds per file load so it really depends on your particular setup.

  9. Harald says:
    February 26, 2006 @ 01:48 — Reply

    Very interesting! 24%... However, I'm quite interested to see how each of the systems was setup and what then the performance increase was. I have the feeling that using an accelerator like Zend or phpAccelerator makes the numbers much lower.

  10. free casino offers online says:
    January 27, 2010 @ 02:39 — Reply

    Comment pending moderation

  11. Edward Clarke says:
    September 11, 2006 @ 11:20 — Reply

    I'd like to know more about the setup too. As mentioned, file operations will hit the performance but requiring more processing than is needed can also be detrimental. It's a fine balance between server load and performance and it would need to be tested in each individual case.

  12. Dan says:
    February 19, 2007 @ 17:18 — Reply

    Isn't it a bit silly having a cpatcha image that says the same phrase every time... I thought it was session based, but tried on 3 different browsers and every time, your captcha code is spamsucks. not very safe really.

  13. Jim Plush says:
    February 19, 2007 @ 17:37 — Reply

    it was an experiment, and it's worked. I used to get hundreds of spam posts a month by spambots. By installing the worlds most simple captcha I eliminated every spam post on my blog. Spammers only care about cracking large sites. They're not concerned with writing a bot specifically for my blog. If they did I would install a better one. I just found it funny that it works, so I'm keeping it for now.

  14. software development uk says:
    October 19, 2009 @ 03:10 — Reply

    Humm... interesting, Some great advie, and has taken away that annoyance and time consuming, downloadin 9 different files... Keep up the good work

  15. Internet casino programs says:
    January 27, 2010 @ 02:36 — Reply

    Comment pending moderation

  16. Canvas Prints says:
    March 1, 2010 @ 09:45 — Reply

    Comment pending moderation

  17. blu ray ripper says:
    April 18, 2010 @ 04:23 — Reply

    Comment pending moderation

  18. virbram five fingers says:
    June 4, 2010 @ 20:49 — Reply

    Comment pending moderation

  19. LOUIS VUIttON Outlet says:
    June 9, 2010 @ 23:00 — Reply

    Comment pending moderation

  20. Replica Jewelry says:
    June 20, 2010 @ 23:22 — Reply

    Comment pending moderation

  21. air max says:
    June 25, 2010 @ 01:43 — Reply

    Comment pending moderation

Leave a Comment

Line and paragraph breaks automatic, HTML allowed: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <code> <em> <i> <strike> <strong>

Comments disabled due to spammers being losers that lead sad lives.