java vs php 基准测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4976295/
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
java vs php benchmark
提问by regality
I'm a php developer, but recently had to write the same application twice, once in php and once in java, for a class I'm taking at school. For curiosity I did a benchmark on the two and found that the java version was 2 to 20 times slower than the php version if the database is accessed, and 1 to 10 times slower without DB access. I see two immediate possibilites:
我是一名 php 开发人员,但最近不得不为我在学校上的一门课编写两次相同的应用程序,一次在 php 中,一次在 java 中。出于好奇,我对两者做了一个基准测试,发现如果访问数据库,java版本比php版本慢2到20倍,不访问DB则慢1到10倍。我看到两个直接的可能性:
- I suck at java.
- I can finally tell people to quit whining about php.
- 我很讨厌java。
- 我终于可以告诉人们不要再抱怨 php 了。
I posted my servlet code here. I don't want any nit-picky whining or minor improvements, but can someone see a horrible glaring performance issue in there? Or can anybody explain why Java feels like it has to suck?
我在这里发布了我的 servlet 代码。我不想要任何挑剔的抱怨或小的改进,但是有人能看到那里有一个可怕的明显的性能问题吗?或者任何人都可以解释为什么 Java 感觉它很糟糕?
I've always heard people say that java is faster and more scalable than php, especially my teacher, he is convinced of it, but the more requests that are made, the slower java gets. php doesn't seem to be affected by increased loads but remains constant.
一直听人说java比php更快,扩展性也更好,尤其是我的老师,他深信不疑,但是请求越多,java越慢。php 似乎不受负载增加的影响,但保持不变。
采纳答案by Tim Bender
In a mature Java web application the Servlet would make use of an existing JDBC connection pool. Establishing a new connection will be by far the greatest cost you pay in time.
在成熟的 Java Web 应用程序中,Servlet 将使用现有的 JDBC 连接池。到目前为止,建立新连接将是您及时支付的最大成本。
Calling Class.forName
for every attempt to get the connection will also cause an unnecessary slow down.
Class.forName
每次尝试获取连接都调用也会导致不必要的减速。
JVM tuning could also be a factor. In an enterprise environment the JVM memory and possibly GC configurations would be adjusted and tuned to achieve a desirable balance between responsiveness and resource utilization.
JVM 调优也可能是一个因素。在企业环境中,JVM 内存和可能的 GC 配置将被调整和调整,以在响应性和资源利用率之间实现理想的平衡。
As Stephen C points out, the JVM also has a concept of a sort of "warm up".
正如 Stephen C 指出的那样,JVM 也有一种“预热”的概念。
All that said, I have no idea how PHP compares to Java and I feel both languages offer great solutions to separate non-disjoint sets of needs.
尽管如此,我不知道 PHP 与 Java 相比如何,而且我觉得这两种语言都提供了很好的解决方案来分离不同的需求集。
回答by typo.pl
Based on not much info (where the best decisions are made), my guess is the Class.forName("com.mysql.jdbc.Driver");
in getConnection()
is the big timesink.
基于没有太多信息(其中最好的作出决定),我的猜测是Class.forName("com.mysql.jdbc.Driver");
在getConnection()
为大timesink。
Creating a new String
in importFile when the char[]
can be passed to out.println is me nitpicking.
new String
当char[]
可以传递给 out.println时创建一个in importFile是我挑剔的。
回答by Foo Bah
Your test seems to reflect initial overhead moreso than steady-state performance. Try doing the non-DB tests multiple times in a loop (so that each test wold run the code multiple times) and look at the linear relationship between runtime and number of iterations. I suspect the incremental cost for java is lower than that for php
您的测试似乎比稳态性能更能反映初始开销。尝试在循环中多次执行非数据库测试(以便每个测试多次运行代码)并查看运行时间和迭代次数之间的线性关系。我怀疑 java 的增量成本低于 php