Jump to content

PHP :: coding style: Performance, opcode, cache, functions, ..


RUBn

Recommended Posts

I have been writing php code for a couple of years now.  I write php code mostly for webservers with a big user load and sometimes even opensource.  Now, the time has come for me to finally look for the answers I have been wondering about since the beginning of my "php career" and have never found back on the internet (unless far from complete).  I should have had this asked much sooner, but you know how it is...

This question is about style of programming or best practice of writing code around a particular feature, that may or may not exist, in php, namely caching opcode.

 

I don't know how to simply ask it, so I'll just start with my style of php as of today:

I never put all my functions in just 1, 2 or 3 files only.  I mostly have 1 file for each function, unless I am sure that if the one function is used, the other will also be used in the php file calling that function.  I even don't include my files at the top of the php file, but in the middle of the code (because of IF's in the page, the function might not be necessary).  I do this because I think that php engine interprets each file every time it is called.  If so, that means I would loose speed performance otherwise because then, each time, unused functions are interpreted, again and again.  Ok, maybe the files itself are cached (by windows, apache or php itself), but is the intermediate code, the opcode cached (as long that there aren't any changes to the file) ?  Because, if the opcode is cached, then I believe I should rethink my style.  Interpreting uses relatively alot of time.

 

My questions:

- Ignoring scalability, have you anything to add to my thinking above ?

- Is opcode cached in php ? 

- If not, are there solutions that really improves speed by caching the intermediate code (opcode) or even further generated code ? (apc cache, ..?)

- If there are, are they easy to install and use ?  Do I need to change the code itself to use them (in contrast to only replacing my functions and files) ?

- Any benchmarks ? Any tools/functions that I can use to check real timing (from moment request arrives at server untill the generated html) ? (Just timing inside php is not enough if you understand my problem)

- What's the general practice in opensource codes (the famous ones) (regarding style and using cache or not) ?

 

I use php (5.1.6) in XAMPP environment on Windows server (SBS).

 

Please try to be short and complete in your suggestions ?  I don't mind links.

 

Big thanks...

http://php.net/apc  ;)

PHP 6 will come with it as a standard built in.

 

Normally what I do is stick with the smallest amount of code and include addons as needed with a loadAddon() function called by the controller or the model constructor, unfortunately this keeps you from deciding the addons to be included per model/controller at runtime (I've never needed to do this). The MVC pattern makes this fairly easy though I don't know how you are used to handling the structure of your apps.

Thanks for the reply..

 

I hope php6 comes soon  :o  Anyway, I guess I have to read up, a bit a lot :D, on apc first.

 

Do you include addons from Open Source small codes or are you talking about like in a cms or so with big (and small) addons ?  But, if I understand you well, these are nót just called when needed at runtime?

I am also a bit interested now in where you get your addons..

 

MVC pattern means M$ Visual C pattern?  Could you explain?  I don't have a clue what you are talking about ???

 

 

P.S. Please don't mind my late posting now and possibly in the future... I am a slow reactor.  Aren't we all so busy?    ;D

MVC: Model-View-Controller, a way structuring code and process in Back-end, controller and display.

 

While I'm not an expert on low-level PHP performance I do believe that there is an overhead and a latency involved with all includes because PHP/Apache must go to the harddrive each time, which is why I think it is better to (at most) separate functions into different files by their purpose or for separate classes. E.g:(A file for text functions/a file for database functions)

 

AFAIK without a byte-code cache the cost of including multiple files is probably relatively high in a high-load environment.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.