What makes PHP slower than Java or C#?

后端 未结 8 753
耶瑟儿~
耶瑟儿~ 2020-12-14 16:10

This is something I\'ve always wondered: Why is PHP slower than Java or C#, if all 3 of these languages get compiled down to bytecode and then executed from there? I know th

相关标签:
8条回答
  • 2020-12-14 16:15

    Depends on what you want to do. In some cases, PHP is definitely faster. PHP is (pretty) good at file manipulation and other basic stuff (also XML stuff). Java or C# might be slower in those cases (though I didn't benchmark).

    Also, the PHP output (HTML or whatever) needs to be downloaded to the browser, which also consumes time.

    Also, the speed of Java / C# is very much depending on the machine it runs on (which could be multiple). Java / C# could be slow on your computer, while PHP just runs on one server from which it is available and is always as fast as the server is (except for download times, etc.).

    I don't think they are comparable in a general manner. I think you need to take a task, which you could be accomplished with those three programming languages, and then compare that. That is basically always what you should do when choosing a programming language; find the one that fits the task. Don't shape the task until it fits the programming language.

    0 讨论(0)
  • 2020-12-14 16:25

    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 !

    0 讨论(0)
  • 2020-12-14 16:26

    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++.

    0 讨论(0)
  • 2020-12-14 16:31

    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.

    0 讨论(0)
  • 2020-12-14 16:31

    According to wikipedia, PHP uses The Zend Engine, which does not have a JIT.

    0 讨论(0)
  • 2020-12-14 16:33

    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.

    0 讨论(0)
提交回复
热议问题