views:

186

answers:

4

I have heard a lot that PHP is slow compared other languages. Is the speed difference noticeable enough that I should switch to another language? And if so what other language would you recommend? Or what would be some good optimizations that could speed up the PHP?

+1  A: 

To speed up PHP, try APC - Alternative PHP Cache.

It can cache the compiled code so the source code files don't need to be reparsed for every request.

More info about APC and other PHP accelerators can be found at Wikipedia.

Mark Byers
+8  A: 

This question comes up a lot. The answer is:

  • Yes it's slower than C#, Java, C/C++, etc.
  • No it probably won't matter.

You can build large scale PHP systems. 4 of the top 20 visited Websites are powered by PHP (Facebook, Yahoo, Wikipedia, Flickr). PHP with an opcode cache (eg APC) can take you much further than you'll probably need or care about.

Most slow Websites have nothing to do with the language they're using. A lot of the time spent on an HTTP request comes down to network latency, absent or ineffectual caching of static resources, lack of compression resulting in more bandwidth used than necessary, poorly performning Javascript and so on.

If you get really desperate for performance you can always use HipHop, which compiles PHP to C++.

cletus
Facebook released a tool for compiling PHP is speed matters that much.
Grant Johnson
Flickr is mostly image data unrelated to PHP so the pressure is put on the web servers to feed data. Wikipedia using caching heavily. Facebook using caching and does heavy lifting on the backend using C++, Java, etc.
Nissan Fan
@Nissan that's kinda the point. CPU is rarely a bottleneck for a Web site.
cletus
Also database access. I've benchmarked my sites, and MySQL queries are always > 90% of execution time.
Brendan Long
+2  A: 

PHP will be plenty fast enough for web site applications if you use best practices.

If you compare PHP to, say C++, of course it will be slower. But you need to consider total cost of development. Just because one language produces faster programs doesn't mean it will be more cost effective. Depending on your programming style, experience, and the project you are working on, you may find that a different language is better suited for the task.

If you use an opcode cache, you will get a very big speed gain simply by removing the need for accessing the disk and parsing the PHP files.

As with any language, you do need to be familiar with the data structures and how they are to be used efficiently. Poor algorithms will be slow regardless of the language, but especially in a scripting language where lots of "magic" happens under the hood.

konforce
A: 

It depends on usage case. Nice example to illustrate this:

  • When you use PHP as server side web scripting language it will be faster than C/C++ program running as a CGI (this is because for CGI a separate process needs to be created and some setup must be done, while PHP scripts are running inside http server module and are just "ready to go")

  • On the other hand, when you use PHP for numerical computation it will be drastically slower than program written in C/C++

PHP is designed to be server side web programming language and for that purpose it should be used. It is reasonably efficient for this task but you can speed it up with caching tools. If even that is not enough, you can write extension in Zend API.

doc