views:

633

answers:

5

Hello

I'd like to have your opinion about writing web apps in PHP vs. a long-running process using tools such as Django or Turbogears for Python.

As far as I know: - In PHP, pages are fetched from the hard-disk every time (although I assume the OS keeps files in RAM for a while after they've been accessed) - Pages are recompiled into opcode every time (although tools from eg. Zend can keep a compiled version in RAM) - Fetching pages every time means reading global and session data every time, and re-opening connections to the DB

So, I guess PHP makes sense on a shared server (multiple sites sharing the same host) to run apps with moderate use, while a long-running process offers higher performance with apps that run on a dedicated server and are under heavy use?

Thanks for any feedback.

A: 

PHP is a language like Java etc. Only your executable is the php binary and not the JVM! You can set another MAX-Runtime for PHP-Scripts without any problems (if your shared hosting provider let you do so).

Where your apps are running shouldn't depend on the kind of the server. It should depend on the ressources used by the application (CPU-Time,RAM) and what is given by your Server/Vserver/Shared Host!

For performance tuning reasons you should have a look at eAccelerator etc. Apache supports also modules for connection pooling! See mod_dbd. If you need to scale (like in a cluster) you can use distributed memory caching systems like memcached!

Martin K.
+1  A: 

PHP is fine for either use in my opinion, the performance overheads are rarely noticed. It's usually other processes which will delay the program. It's easy to cache PHP programs with something like eAccelerator.

me_here
+2  A: 
  • With APC, which is soon to be included by default in PHP compiled bytecode is kept in RAM.
  • With mod_php, which is the most popular way to use PHP, the PHP interpreter stays in web server's memory.
  • With APC data store or memcache, you can have persistent objects in RAM instead of for example always creating them all anew by fetching data from DB.

In real life deployment you'd use all of above.

vartec
The best solution uses both of these, or alternatively substituting mod_php with a FastCGI setup.
R. Bemrose
of course, by no means I'd suggest that these are alternatives
vartec
On a properly configured webserver, the I/O will be cached - so even without APC/mod_php you're unlikely to be hitting the disk. I would suggest using mod_php over fast CGI over CGI though (@Powerlord: mod_PHP will run faster than FastCGI). There's no way to avoid re-reading the session although you can use a faster storage medium like memcached.
symcbean
+1  A: 

After you apply memcache, opcode caching, and connection pooling, the only real difference between PHP and other options is that PHP is short-lived, processed based, while other options are, typically, long-lived multithreaded based.

The advantage PHP has is that its dirt simple to write scripts. You don't have to worry about memory management (its always released at the end of the request), and you don't have to worry about concurrency very much.

The major disadvantage, I can see anyways, is that some more advanced (sometimes crazier?) things are harder: pre-computing results, warming caches, reusing existing data, request prioritizing, and asynchronous programming. I'm sure people can think of many more.

Most of the time, though, those disadvantages aren't a big deal. You can scale by adding more machines and using more caching. The average web developer doesn't need to worry about concurrency control or memory management, so taking the minuscule hit from removing them isn't a big deal.

Richard Levasseur
A: 

As many others have noted, PHP nor Django are going to be your bottlenecks. Hitting the hard disk for the bytecode on PHP is irrelevant for a heavily trafficked site because caching will take over at that point. The same is true for Django.

Model/View and user experience design will have order of magnitude benefits to performance over the language itself.

Adam Nelson