Что делает PHP медленнее, чем Java или C#?

Адаптированный от здесь , основываясь на ответе scunliffe:

function getStyle(className) {
    var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
    for (var x = 0; x < classes.length; x++) {
        if (classes[x].selectorText == className) {
            (classes[x].cssText) ? alert(classes[x].cssText) : alert(classes[x].style.cssText);
        }
    }
}
getStyle('.test');

26
задан 4 revs, 2 users 100% 10 September 2009 в 21:08
поделиться

8 ответов

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 (though see Facebook's HHVM for an example of a third-party JIT for PHP).

39
ответ дан 28 November 2019 в 06:58
поделиться

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.

9
ответ дан 28 November 2019 в 06:58
поделиться

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.

4
ответ дан 28 November 2019 в 06:58
поделиться

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

5
ответ дан 28 November 2019 в 06:58
поделиться

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 :


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 !

2
ответ дан 28 November 2019 в 06:58
поделиться

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.

-2
ответ дан 28 November 2019 в 06:58
поделиться

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

-3
ответ дан 28 November 2019 в 06:58
поделиться

Интерпретируемый язык не может быть быстрее, чем скомпилированный язык или даже язык JIT в тривиальных условиях.

Если только не ваша тестовая программа состоит из вывода «Hello Worlds», если вас беспокоит скорость, придерживайтесь C # или Java.

0
ответ дан 28 November 2019 в 06:58
поделиться
Другие вопросы по тегам:

Похожие вопросы: