java 与 php 基准测试

发布于 2024-10-17 05:30:12 字数 457 浏览 4 评论 0原文

我是一名 php 开发人员,但最近不得不为我在学校上的课程编写两次相同的应用程序,一次用 php,一次用 java。出于好奇,我对两者进行了基准测试,发现如果访问数据库,java 版本比 php 版本慢 2 到 20 倍,而没有访问数据库则慢 1 到 10 倍。我看到两个直接的可能性:

  1. 我不擅长java。
  2. 我终于可以告诉人们不要再抱怨 php 了。

我在此处发布了我的servlet代码。我不想要任何挑剔的抱怨或微小的改进,但是有人能看到其中存在可怕的明显性能问题吗?或者谁能​​解释一下为什么 Java 感觉很糟糕?

我总是听人说java比php更快、更具可扩展性,尤其是我的老师,他对此深信不疑,但是发出的请求越多,java就越慢。 php 似乎没有受到负载增加的影响,但保持不变。

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:

  1. I suck at java.
  2. I can finally tell people to quit whining about 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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2024-10-24 05:30:12

在成熟的 Java Web 应用程序中,Servlet 将利用现有的 JDBC 连接池。建立新的连接将是迄今为止您所付出的最大的时间成本。

每次尝试获取连接时调用 Class.forName 也会导致不必要的速度减慢。

JVM 调整也可能是一个因素。在企业环境中,JVM 内存和可能的 GC 配置将进行调整和调优,以在响应能力和资源利用率之间实现理想的平衡。

正如 Stephen C 指出的那样,JVM 也有一种“预热”的概念。

话虽如此,我不知道 PHP 与 Java 相比如何,而且我觉得这两种语言都提供了很好的解决方案来分离不相交的需求集。

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.

Calling Class.forName for every attempt to get the connection will also cause an unnecessary slow down.

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.

As Stephen C points out, the JVM also has a concept of a sort of "warm up".

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.

对不⑦ 2024-10-24 05:30:12

基于不多的信息(做出最佳决策的地方),我的猜测是 getConnection()Class.forName("com.mysql.jdbc.Driver"); > 是一个很大的时间沉没。

char[] 可以传递给 out.println 时,在 importFile 中创建一个 new String 是我的挑剔。

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.

Creating a new String in importFile when the char[] can be passed to out.println is me nitpicking.

多情癖 2024-10-24 05:30:12

您的测试似乎更多地反映了初始开销而不是稳态性能。尝试在循环中多次执行非数据库测试(以便每个测试都会多次运行代码),并查看运行时间和迭代次数之间的线性关系。我怀疑java的增量成本比php低

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文