Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes PHP slower than Java or C#?

People also ask

Is PHP slower than Java?

Design comparison From a design and architecture perspective, Java is a compiled language and is faster than PHP. Nevertheless, when we put the two languages into practice, PHP is faster than Java on the web. PHP is designed for the web and is the most advanced, mainstream and server-side content.

Why is PHP so slow?

So why is PHP slow in comparison with some other languages? PHP is a dynamic, interpreted language. This means that it is not compiled to machine language but rather read at runtime. PHP also has a share-nothing architecture, so on every request it interprets everything from fresh.

Which is faster Java or PHP?

In a nutshell: While comparing the Java vs PHP performance, Java is clearly a winner as it is faster and efficient than PHP to write enterprise applications. Like developers have to build mobile enterprise applications to streamline complex business processes and operations.

Which is faster C# or PHP?

ASP.net is usually written in C# (pronounced C Sharp) — generally speaking, as of this writing, C# is a faster programming language than PHP.


One reason is the lack of a JIT compiler in PHP, as others have mentioned.

Another big reason is PHP's dynamic typing. A dynamically typed language is always going to be slower than a statically typed language, because variable types are checked at run-time instead of compile-time. As a result, statically typed languages like C# and Java are going to be significantly faster at run-time, though they typically have to be compiled ahead of time. A JIT compiler makes this less of an issue for dynamically typed languages, but alas, PHP does not have one built-in. (Edit: PHP 8 will come with a built-in JIT compiler.)


I'm guessing you are a little bit into the comparing of apples and oranges here - assuming that you are using all these languages to create web applications there is quite a bit more to it than just the language. (And lots of the time it is the database that is slowing you down ;-)

I would never suggest choosing one of these languages over the other on the basis of a speed argument.


Both Java and C# have JIT compilers, which take the bytecode and compile into true machine code. The act of compiling it can take time, hence C# and Java can suffer from slower startup times, but once the code is JIT compiled, its performance is in the same ballpark as any "truly compiled" language like C++.


The biggest single reason is that Java's HotSpot JVM and C#'s CLR both use Just-In-Time (JIT) compilation. JIT compilation compiles the bytecodes down to native code that runs directly on the processor.

Also I think Java bytecode and CIL are lower-level than PHP's internal bytecode which might make alot of JIT optimizations easier and more effective.


A wild guess might be that JAVA depends on some kind of "application" server, while PHP doesn't -- which means a new environnement has to be created each time a PHP page is called.

(This was especially true when PHP was/is used as a CGI, and not as an Apache module or via FastCGI)


Another idea might be that C# and JAVA compilers can do some heavy optimisations at compile time -- on the other side, as PHP scripts are compiled (at least, if you don't "cheat" with an opcode cache) each time a page is called, the compilation phase has to be real quick ; which means it's not possible to spend much time optimizing.


Still : Each version of PHP generally comes with some amelioration of the performances ; for instance, you can gain between 15% and 25% of CPU, when switching from PHP 5.2 to 5.3.

For instance, take a look at those benchmarks :

  • Benchmark of PHP Branches 3.0 through 5.3-CVS
  • Performance PHP 5.2 vs PHP 5.3 - huge gain
  • Bench PHP 5.2 vs PHP 5.3 -- disclaimer : it's in french, and I'm the one who did it.


One important thing, also, is that PHP is quite easy to scale : just add a couple of web servers, and voila !

The problem you often meet when going from 1 to several servers is with sessions -- store those in DB or memcached (very easy), and problem solved !


As a sidenote : I would not recommend choosing a technology because there is a couple of percent difference of speed on some benchmark : there are far more important factors, like how well your team know each technology -- or, even, the algorithms you are going to use !