是什么让 PHP 比 Java 或 C# 慢?
这是我一直想知道的事情:如果所有 3 种语言都编译为字节码然后从那里执行,为什么 PHP 比 Java 或 C# 慢?我知道通常 PHP 会根据每个请求重新编译每个文件,但即使将 APC(字节码缓存)引入其中,其性能也远不及 Java 或 C#(尽管 APC 大大提高了它)。
编辑: 我什至没有在网络层面上谈论这些语言。我说的是他们在进行数字运算时的比较。甚至不包括启动时间或类似的时间。
另外,我不会根据这里的回复做出某种决定。 PHP 是我选择的语言;我只是对它的设计感到好奇。
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 that normally PHP recompiles each file with each request, but even when you bring APC (a bytecode cache) into the picture, the performance is nowhere near that of Java or C# (although APC greatly improves it).
Edit:
I'm not even talking about these languages on the web level. I am talking about the comparison of them when they're number crunching. Not even including startup time or anything like that.
Also, I am not making some kind of decision based on the replies here. PHP is my language of choice; I was simply curious about its design.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如其他人提到的,原因之一是 PHP 中缺少 JIT 编译器。
另一个重要原因是 PHP 的动态类型。动态类型语言总是比静态类型语言慢,因为变量类型是在运行时而不是编译时检查的。因此,C# 和 Java 等静态类型语言在运行时的速度将显着加快,尽管它们通常必须提前编译。对于动态类型语言来说,JIT 编译器使这个问题不再那么严重,但遗憾的是,PHP 没有内置编译器。 (编辑:PHP 8 将带有内置的 JIT 编译器。)
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.)
我猜您有点喜欢在这里比较苹果和橘子 - 假设您正在使用所有这些语言来创建 Web 应用程序,那么不仅仅是语言本身。 (很多时候是数据库拖慢了你的速度;-)
我绝不会建议基于速度论据来选择其中一种语言。
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.
Java 和 C# 都有 JIT 编译器,它获取字节码并编译成真正的机器代码。编译它的行为可能需要时间,因此 C# 和 Java 可能会遇到启动时间较慢的问题,但是一旦代码经过 JIT 编译,其性能与任何“真正编译”的语言(如 C++)大致相同。
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++.
最大的一个原因是Java的HotSpot JVM和C#的CLR都使用Just-In-Time (JIT)编译。 JIT 编译将字节码编译为直接在处理器上运行的本机代码。
另外,我认为 Java 字节码和 CIL 比 PHP 的内部字节码级别更低,这可能会使大量 JIT 优化更容易、更有效。
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.
一个大胆的猜测可能是,JAVA 依赖于某种“应用程序”服务器,而 PHP 则不然——这意味着每次调用 PHP 页面时都必须创建一个新的环境。
(当 PHP 被用作 CGI,而不是 Apache 模块或通过 FastCGI 时尤其如此)
另一个想法可能是 C# 和 JAVA 编译器可以在编译时进行一些大量的优化——另一方面,因为 PHP 脚本是编译的(至少,如果你不使用操作码缓存“作弊”的话)< /em> 每次调用页面时,编译阶段都必须非常快;这意味着不可能花费太多时间进行优化。
尽管如此:PHP 的每个版本通常都会对性能进行一些改进;例如,从 PHP 5.2 切换到 5.3 时,您可以获得 15% 到 25% 的 CPU 利用率。
例如,看一下这些基准:
还有一件重要的事情是 PHP 非常容易扩展:只需添加几个 Web 服务器,瞧!
从一台服务器转移到多台服务器时,您经常遇到的问题是会话 - 将会话存储在数据库或内存缓存中(非常简单),问题就解决了!
作为旁注:我不建议选择一种技术,因为某些基准测试的速度存在百分之几的差异:还有更重要的因素,例如您的团队对每种技术的了解程度,甚至是您所使用的算法即将使用!
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 !
在简单的条件下,解释型语言不可能比编译型语言甚至 JIT 语言更快。
除非您的测试程序包括打印“Hello Worlds”,如果您担心速度,请坚持使用 C# 或 Java。
There is no way an interpreted language can be faster than a compiled language or even a JIT language under trivial conditions.
Unless your test program consists of printing out "Hello Worlds" if you are concerned about speed, stick with C# or Java.
取决于你想做什么。在某些情况下,PHP 绝对更快。 PHP(非常)擅长文件操作和其他基本内容(还有 XML 内容)。在这些情况下,Java 或 C# 可能会更慢(尽管我没有进行基准测试)。
此外,PHP 输出(HTML 或其他)需要下载到浏览器,这也消耗时间。
此外,Java / C# 的速度很大程度上取决于它运行的机器(可能是多个)。 Java / C# 在您的计算机上可能会很慢,而 PHP 仅在一台可用的服务器上运行,并且始终与服务器一样快(下载时间等除外)。
我不认为它们在一般意义上具有可比性。我认为你需要接受一项可以用这三种编程语言完成的任务,然后进行比较。这基本上是您在选择编程语言时应该做的事情;找到适合该任务的那个。在适合编程语言之前不要塑造任务。
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.
根据 wikipedia,PHP 使用 Zend 引擎,没有 JIT。
According to wikipedia, PHP uses The Zend Engine, which does not have a JIT.