View Full Version : Confusion about PHP performance

12-16-2008, 09:08 PM

I want to ask you about php performance. Not that I have any problems running my php scripts. I'm perfectionist and I just can't sleep if some things are not clear to me.

Does parsed and compiled php code get cached somewhere or the server has to parse the unchanged code over and over again for each request? How important it is to separate the code with includes to eliminate all unnecessary code?

The way I write php right now is to have 2-5 includes with helper functions (database, url functions, some functions for string cleansing and error handler) which are used on almost every request. The rest of the code is included as needed. I usually have about 50-200 php files in my projects. Each request needs only 1-5 small files.

Also I don't use OOP in php. I use includes, functions and if/else statements are the paths to the right includes. I never had a situation where I have felt that I should use OOP. I don't have any "objects" in my code. I have only one instance of an "object" usually so the underscore character can replace all "->"'s. I don't care about restricting variable scopes either. It's my all my code. Arrays and functions are my friends. If I had a class with 1000 lines of code, I would still need only 1-2 member functions of a class on a single request. It's not a program that is loaded into the memory once on a start-up.

So give me some advice, please. How to write fastest php code and whats the best way to do it? The most important thing I want to hear about is the caching of the compiled code.


12-17-2008, 07:50 AM
Interesting questions. I'll try to answer to the best of my knowledge.
Best I know, PHP pages are always interpreted on any page load (assuming module through apache). The difference will come from caching that's performed by the browser, proxy and possibly the web server. Regardless of its been compiled into bytecode or not. This differs from languages like JSP which are loaded into memory to handle requests. These are also IMO less stable than singular calls, though they are a lot faster. My guess is that a spark system would actually run these like a dream though, but a JSP programmer could tell you more (I don't do a lot of JSP at all).

I would also opt for more files with less code from an optimized point of view. This limits the amount of repetative code, and allows you to reuse based on limited code choice. This creates a new problem though - simply remembering which files have which functions within them. I'm moving towards more OO php designs, so more files is an after result. The OO approach has the slight advantage of generally the file naming indicates the object. They increase the reusability of the code (more so even than standard function calls), but does take more memory to parse. Technically, in PHP OO programming is a much worse idea than procedurally from an optimization point of view.

Unlike languages like C, PHP has little by way of optimization capabilities apart from code reuse and sound logic control. PHP doesn't allow you to manipulate memory (except the values with use of references, which are not really pointers), and doesn't let you pack objects in a way that optimizes fetches (like structs do). You kinda gotta rely on the garbage collector.

Things a lot of people ignore is that PHP only cleans up memory once the script terminates. So, if you're done with a variable or resource, destroy it in script so you can release memory for reuse. Otherwise, it will hold it until the end. This is generally not a problem though, with the exception of huge resultsets and image manipulations (or of course really tiny memory space alloted to PHP).

Hopefully that gives you an idea! Sadly my answer is limited to my lack of PHP understanding (not usage, but how it actually operates under the hood). A question like this may be better answered by the zend developers.

12-17-2008, 02:20 PM
Good answer!
I'm asking this because I want to change my coding style but I don't want to start rewriting whole programs if I run into performance problems because of that. Right now I'm doing too much optimizing. I don't have problems with naming or remembering all my files and functions. Problems arise when I start changing, renaming or reorganizing my functions, code or file structure. This isn't good. I just found out that there is autoload function in php which is called when trying to create an object of an undefined class. I think I should start using some OOP. I read that PHP parser is pretty fast and some extra code won't affect performance much.

12-17-2008, 03:59 PM
Its definitely less of a problem nowadays.
My old linux machine was running 32MB of ram (out of commission only starting the end of last year) and I had no problems with it. Granted, it was specifically compiled as a web server, but even with OO code it didn't slow it down significantly.
Computers are so fast nowadays. You may want to consider implementing more object oriented code into you're work, and slowly phase out standard procedural functions. Autoload is very convenient, but remember that it is a last ditch attempt at construction - if it fails, you cannot capture any errors or exceptions cast from it, it will always terminate you're scripts. Always remember though, in PHP anything that can be done through objects can also be done in procedural code. Often this is less code and less work, though also less portable.
Programming is all about the fine lines and tradeoffs.