Java 与 PHP 速度比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3315572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:00:36  来源:igfitidea点击:

Java vs. PHP Speed Comparison

javaphpperformance

提问by

Which language is faster for web, Java or PHP?

对于 Web、Java 或 PHP,哪种语言更快?

回答by Josh K

Speed doesn't matter

速度无所谓

in most cases.

大多数情况下。

Processing is cheap. Code in what you're comfortable with. Writing proper code goes much further for speed then choosing a language. Solid coding conventions and design plan will also help more.

加工便宜。用你觉得舒服的方式编码。编写正确的代码比选择语言更能提高速度。扎实的编码约定和设计计划也将提供更多帮助。

回答by Ryan

Best answer I could find

我能找到的最佳答案

"stuff to consider:

"要考虑的事情:

  1. Java web applications are compiled to bytecode. Even JSPs, which are compiled at runtime. This is an advantage over most uses of PHP, where the Zend Optimizer is not in use.

  2. Data can be cached in a live servlet instance - no direct/easy way of doing this in PHP to my knowledge (there is only ever a single instance of a servlet/JSP in memory)

    • If anybody knows how to cache data in PHP without resorting to ugly hacks, please enlighten me!
  3. Java applications tend to be n-tiered, which generally results in a more maintainable application at a slight performance penalty. This probably sounds trollish, but honestly: even within Java itself direct use of JDBC will always be faster than going through three layers of objects to the database.

  1. Java Web 应用程序被编译为字节码。甚至是在运行时编译的 JSP。与 PHP 的大多数用途相比,这是一个优势,其中 Zend Optimizer 未在使用中。

  2. 数据可以缓存在实时 servlet 实例中 - 据我所知,在 PHP 中没有直接/简单的方法可以做到这一点(内存中只有一个 servlet/JSP 实例)

    • 如果有人知道如何在 PHP 中缓存数据而不诉诸丑陋的黑客,请赐教!
  3. Java 应用程序往往是 n 层的,这通常会导致更易于维护的应用程序,但性能略有下降。这听起来可能有点荒谬,但老实说:即使在 Java 本身中,直接使用 JDBC 也总是比通过三层对象访问数据库要快。

But is an n-tiered Java application able to hit the database sooner than an uncompiled, hacked-up monolithic PHP script? I don't think there's an answer to that question.

但是,n 层 Java 应用程序是否能够比未编译的、经过修改的单体 PHP 脚本更快地访问数据库?我认为这个问题没有答案。

All that said, I'm working on an n-tiered MVC framework for php 5 (it's called Pure (http://www.sf.net/projects/php-pure)), so my PHP applications are generally n-tiered too. I'll worry about speed when and if it becomes an issue. For now, it's definitely not an issue."

综上所述,我正在为 php 5 开发一个 n 层 MVC 框架(它被称为 Pure(http://www.sf.net/projects/php-pure)),所以我的 PHP 应用程序通常是 n 层的也。当速度成为问题时,我会担心速度。就目前而言,这绝对不是问题。”

courtesy of krumms

克鲁姆斯的礼貌

回答by thomasrutter

Can't answer this question with one or the other unless you define what you want to measure the speed of.

除非您定义要测量的速度否则无法用一个或另一个来回答这个问题。

Some things are much faster in PHP (in a native function for example), other things are much faster in Java.

有些事情在 PHP 中要快得多(例如在本机函数中),而其他事情在 Java 中要快得多。

回答by Jerry Coffin

The intent of each language is substantially different from the other, so if you're debating over which to use for a particular task, you should generally based the decision on that task (and how well suited each language is to it) rather than performance.

每种语言的意图都与其他语言大不相同,因此,如果您正在争论将哪种语言用于特定任务,您通常应该根据该任务(以及每种语言对它的适合程度)而不是性能来做出决定.

For raw performance of code written in the language (as opposed to simply calling code in the standard library), Java will probably run faster than PHP as an extremely general rule. If that matters, chances are that PHP just isn't very well suited to the task at hand.

对于用该语言编写的代码的原始性能(而不是简单地调用标准库中的代码),Java 可能会比 PHP 运行得更快,这是一个非常普遍的规则。如果这很重要,很可能是 PHP 不太适合手头的任务。

回答by Truong Ha

Speed aside, I believe the performance of Java is better than PHP. But developing a project in PHP is faster.

抛开速度不谈,我相信Java的性能比PHP好。但是用 PHP 开发项目会更快。

回答by James Anderson

It is a difficult one to answer as in theory Java should be faster: it's precompiled, any trivial algorithim will run faster in Java than PHP and there has been a vast amount of work done to optimise Java from improving the code, standard libraries, to JIT compilers, etc.

这是一个很难回答的问题,因为理论上 Java 应该更快:它是预编译的,任何微不足道的算法在 Java 中都比 PHP 运行得更快,并且已经做了大量工作来优化 Java,从改进代码、标准库到JIT 编译器等

PHP is loaded and interpreted every time if you're not using the Zend optimiser, objects are intialised on every execution, even the most trivial string varaible is actually a complex object with many methods to support.

如果您不使用 Zend 优化器,每次都会加载和解释 PHP,每次执行时都会初始化对象,即使是最简单的字符串变量实际上也是一个复杂的对象,需要支持许多方法。

The problem is that in practice PHP sites seem to run faster using fewer resources.

问题是实际上 PHP 站点似乎使用更少的资源运行得更快。

I think this is because PHP developers take a more straightforward approach to design and don't get lost trying to implement exotic design patterns and implementing endless pointless abstractions.

我认为这是因为 PHP 开发人员采用更直接的设计方法,并且不会在尝试实现奇异的设计模式和实现无休止的无意义抽象时迷失方向。