13

I saw this post on Sitepoint quoting a statement by Rasmus Lerdorf which goes (according to Sitepoint) as follows:

How can you make PHP fast? Well, you can’t" was his quick answer. PHP is simply not fast enough to scale to Yahoo levels. PHP was never meant for those sorts of tasks. "Any script based language is simply not fast enough". To get the speed that is necessary for truly massive web systems you have to use compiled C++ extensions to get true, scaleable architecture. That is what Yahoo does and so do many other PHP heavyweights.

Intrigued by the statement (not to mention the fact that up to now, all I was doing in PHP was small database-based applications), I was wondering how I could "use compiled C++ extensions" with PHP.

Any ideas or resources?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yretuta
  • 7,963
  • 17
  • 80
  • 151
  • >> up to now, all I was doing in PHP was small database-based apps << And for those PHP should be just fine ;-) – igouy Apr 10 '10 at 15:20

7 Answers7

15

Don't even bother. PHP is slow... You may create a mixture of C++ and PHP but you'll need to do lots of profiling to understand what is slow. And this is mostly... PHP.

See following:

Just write in C++ in first place. It is as simple as writing in PHP with modern C++ web framework and good knowledge in C++.

Where to start:

  • CppCMS - scalable MVC framework oriented for performance.
  • Wt - framework that mimics Qt for web (not sure how it scales well).
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
Artyom
  • 31,019
  • 21
  • 127
  • 215
  • 3
    I have made a website in C++; it was very pleasurable to do as the C++ tools (IDEs, debuggers, profilers, Visual Studio, Intellisense, etc) are much much much better than PHP's. The only annoyance was compile times. – Andreas Bonini Apr 10 '10 at 09:19
  • +1 but many more plus ones if you can give more details. Sounds extremely interesting. Website in C++ ... – Mawg says reinstate Monica Apr 10 '10 at 09:35
  • @mawg -- just follow the CppCMS link... there are many details, rationale etc etc. – Artyom Apr 10 '10 at 10:29
7

Slow is subjective term. Facebook (the biggest site online) is built on PHP and I never heard them complain. In the contrary in their pursuit of speed the developed and recently released HipHop If you are going for ultrafast PHP this is where you should focus. C++ for web is kind of impractical because it needs to be compiled and it will slowdown the development process.

Because: Why are so many web languages interpreted rather than compiled?

http://www.bitsandbuzz.com/article/compiled-web-vs-interpreted-web/

Community
  • 1
  • 1
Ivo Sabev
  • 5,230
  • 1
  • 26
  • 38
  • 3
    Note- HipHop only increased the performance in about 2 times because PHP is dynamically typed languages and has no natural way to be compiled into C++... So they have big code base in PHP and stuck with it... – Artyom Apr 10 '10 at 08:49
  • 7
    @Artyom - I'd really hate to be "stuck" with a code base that was making hundreds of millions of dollars. – zombat Apr 10 '10 at 09:09
  • People should understand the difference between programming for web and programming for a home computer. Programming for a home computer puts speed first that is why strongly typed low level languages like C and C++ are preferred, because they give you the highest level of speed optimization. While on the counterpart programming for web is more concerned about making the programming process easier and faster. The truth is that when we talk about web it is cheaper to add a server to speed up bad code than hire a good programmer to write faster code. – Ivo Sabev Apr 10 '10 at 14:45
  • @Artyom Facebook will be stuck if they cannot scale their code, but they can. – Ivo Sabev Apr 10 '10 at 14:45
  • >> strongly typed variables and don't get me started on that topic... << Ur/Web http://www.impredicative.com/ur/ – igouy Apr 10 '10 at 15:26
  • @Billy Corrected: compiled languages are impractical – Ivo Sabev Apr 10 '10 at 17:27
  • @igouy I posted two links answering your "because" – Ivo Sabev Apr 10 '10 at 19:42
  • @Ivo Sabev. Now you say "kind of impractical" rather than impractical. That's kind of backing away. – igouy Apr 11 '10 at 14:52
  • @igouy obviously by pointing out Facebook's work I agree compiling is sometimes practical. What about your mom's blog is it practical to be recompiled on every change? – Ivo Sabev Apr 11 '10 at 16:20
  • For me; Facebook is Full of JS. For me; is slow. It takes forever to completely load. – Elias Nicolas Oct 16 '15 at 17:37
5

The bottleneck is usually I/O or database queries rather than what language you're using.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lotus Notes
  • 6,302
  • 7
  • 32
  • 47
3

The first question that springs to mind is: do you develop for a website processing more than a million hits per hour? Because if not, then your costs are all in software development, and you'll get more bang for your buck by tuning your current PHP code and adding hardware than migrating even part of the codebase to C or C++.

See also http://shootout.alioth.debian.org/ note the comparisons of code size.

Yes, above this level (approximately), then your hardware costs start escalating to the point where it's worth the extra effort of developing in a compiled language.

The next thing to bear in mind is that while it's true that a very well written compiled code will almost always outperform a similarly well written interpreted code, when the code quality is even slightly less than perfect, the difference reduces - and the effect will vary depending on the language as well as the programmers ability. I currently look after a competently developed web application written in Java (which runs as native code) which requires twice the hardware resource to run slower than a comparable system I've managed, written in PHP by programmers with might be best described as quite a wide range of skill levels. Although this directly contradicts what most programmers would think of as common sense, I'm confident in saying that the interpreted/compiled argument is not the only one. There are lots of orgranisations/individuals out there still developing interpreted langaues, e.g. Groovy, Apex.

C.

symcbean
  • 47,736
  • 6
  • 59
  • 94
2

There's an introduction to writing php extensions at http://devzone.zend.com/article/1021 (published in 2005, might be a bit outdated).
You can also use tools like swig to build wrappers for existing C/C++ code.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
1

Thanks to Artyom for the link to this informative link,

http://idleprocess.wordpress.com/2009/11/24/presentation-summary-high-performance-at-massive-scale-lessons-learned-at-facebook/

which says, among much else, "C++ Interoperability Challenging."

C++ requires much effort to write a website. Why not pick a language between C++ and PHP, say Python which is executed by bytecode JIT, and I understand is relatively easily extended by C++.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 1
    Have you tried benchmarking IronPython for real applications? It is not much faster then Python but still much slower then C++. Why? Python dynamically typed language. Same reason HipHop give only x2 gain for PHP applications compiled to C++. – Artyom Apr 10 '10 at 10:31
  • @Artyom: but is it faster than PHP? – Potatoswatter Apr 10 '10 at 17:27
0

For large calculation and processing C++ can be used with php. And for lighter process only PHP is enough.

While working on data processing or calculation based applications C++ can help. Please not you have to use one trick here.

One need to create server(daemon) process which keep listerning for the request. This daemon process is already compiled and it contains processing code.

So when there is any request to process the data it will do it much faster then PHP and reply back with result.

One can use exec or similar function to execute daemon executable.

user3245689
  • 149
  • 1
  • 1
  • 11