Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better performance in PHP?

I generally include 1 functions file into the hader of my site, now this site is pretty high traffic and I just like to make every little thing the best that I can, so my question here is,

Is it better to include multiple smaller function type files with just the code that's needed for that page or does it really make no difference to just load it all as 1 big file, my current functions file has all the functions for my whole site, it's about 4,000 lines long and is loaded on every single page load sitewide, is that bad?

like image 683
JasonDavis Avatar asked Aug 01 '09 23:08

JasonDavis


People also ask

Is PHP getting faster?

Competition is helping drive big performance gains in PHP. Alternative ways of running PHP are becoming viable, accelerating speed. PHP isn't the fastest language for writing web applications, yet we continue to do use it for many other reasons.

Why is PHP so fast?

PHP codes runs much faster than ASP because it runs in its own memory space while ASP uses an overhead server and a COM based architecture. Less Expensive Software – In working with PHP, most tools associated with the program are open source software, such as WordPress, so you need not pay for them.


2 Answers

It's difficult to say. 4,000 lines isn't that large in the realms of file parsing. In terms of code management, that's starting to get on the unwieldy side, but you're not likely to see much of a measurable performance difference by breaking it up into 2, 5 or 10 files, and having pages include only the few they need (it's better coding practice, but that's a separate issue). Your differential in number-of-lines read vs. number-of-files that the parser needs to open doesn't seem large enough to warrant anything significant. My initial reaction is that this is probably not an issue you need to worry about.

On the opposite side of the coin, I worked on an enterprise-level project where some operations had an include() tree that often extended into the hundreds of files. Profiling these operations indicated that the time taken by the include() calls alone made up 2-3 seconds of a 10 second load operation (this was PHP4).

like image 76
zombat Avatar answered Oct 12 '22 21:10

zombat


If you can install extensions on your server, you should take a look at APC (see also).
It is free, by the way ;-) ; but you must be admin of your server to install it ; so it's generally not provided on shared hosting...

It is what is called an "opcode cache".

Basically, when a PHP script is called, two things happen :

  • the script is "compiled" into opcodes
  • the opcodes are executed

APC keeps the opcodes in RAM ; so the file doesn't have to be re-compiled each time it is called -- and that's a great thing for both CPU-load and performances.


To answer the question a bit more :

  • 4,000 lines is not that much, speaking of performances ; Open a couple of files of any big application / Framework, and you'll rapidly get to a couple thousand of lines
  • a really important thing to take into account is maintenability : what will be easier to work with for you and your team ?
  • loading many small files might imply many system calls, which are slow ; but those would probably be cached by the OS... So probably not that relevant
  • If you are doing even 1 database query, this one (including network round-trip between PHP server and DB server) will probably take more time than the parsing of a couple thousand lines ;-)
like image 33
Pascal MARTIN Avatar answered Oct 12 '22 20:10

Pascal MARTIN