简体   繁体   English

关于PHP和Java的问题

[英]Questions about PHP and Java

I want to know how Java (JSP) on Tomcat compares to PHP on Apache in terms of performance. 我想知道Tomcat上的Java(JSP)在性能方面与Apache上的PHP相比如何。

Two servers with the same hardware configurations, one running Tomcat/Java (JSP) the other Apache/PHP, both servers maxed out with how many connections they can handle at once. 两台具有相同硬件配置的服务器,一台运行Tomcat / Java(JSP),另一台运行其他Apache / PHP,两台服务器最多可以同时处理多少个连接。 Would they be somewhat close or would one pull away from the other one by a large margin? 它们会有点接近,还是会大幅度离开另一个? I basically just want to know if Tomcat/Java (JSP) is going to be a big performance hit if I switch to it vs PHP. 我基本上只是想知道如果我切换到它而不是PHP,Tomcat / Java(JSP)是否会成为一个重大的性能影响。 If anyone can give a detailed answer on why one is faster than the other that would be amazing. 如果有人能够详细解答为什么一个比另一个更快,那将是惊人的。 Links are great too, I was unable to find anything online surprisingly. 链接也很棒,我无法在网上找到任何令人惊讶的内容。

Please no Java vs PHP wars, this is about performance only, nothing to do with the languages themselves. 请不要Java vs PHP战争,这只是关于性能,与语言本身无关。

Note: If there is any other concerns I should have for switching to Java from PHP please let me know. 注意:如果从PHP切换到Java应该有任何其他问题,请告诉我。 I REALLY hate asking this question because I'm usually the first person to say "program in what you like" but in my situation I need whats also good for the projects I work for. 我真的很讨厌问这个问题,因为我通常是第一个说“你喜欢的节目”的人,但在我的情况下,我需要什么对我工作的项目也有好处。 I know that there are large sites written in JSP, but it doesn't mean that they're better. 我知道有一些用JSP编写的大型网站,但这并不意味着它们会更好。

Thanks 谢谢

What's good for the projects you're working on is to spend as little time as possible to write them as developer time is way more expensive than any perceived differences in performance. 什么是适合你的工作项目是花尽可能少的时间尽可能把它们写为开发时间的方式比任何性能上的差异感觉更加昂贵。 So stick with what you're familiar with. 所以坚持你熟悉的东西。

The answer to your question is: they are both fast enough. 你的问题的答案是:它们都足够快。

Any such comparison is hard because you end up doing things differently in different languages. 任何这样的比较都很难,因为你最终会以不同的语言做不同的事情。 Java bytecode is probably faster to interpret but then again any decent PHP install uses as opcode cache largely negating any such advantage in real terms. Java字节码可能更快解释,但是任何体面的PHP安装再次使用作为操作码缓存在很大程度上否定了实际上的任何这样的优势。

Java also has a more complicated development model because Web processes are persistent. Java也有一个更复杂的开发模型,因为Web进程是持久的。 This can have a performance advantage but also can create problems like memory and other resource leakage, which PHP doesn't tend to have because everything is created and destroyed on each request (barring session information, memcache and so on). 这可以带来性能优势,但也可能会产生内存和其他资源泄漏等问题,而PHP往往不会因为每个请求都创建和销毁所有内容(禁止会话信息,内存缓存等)。

Also PHP extensions can be created for any parts that you want to speed up. 此外,还可以为要加速的任何部分创建PHP扩展。

$10,000 can buy an awful lot of hardware. 10,000美元可以购买大量硬件。 It can buy the hardware to run SO. 它可以购买硬件来运行SO。 It doesn't buy much developer time. 它不会花太多开发时间。

I've got experience doing both Java and PHP development. 我有从事Java和PHP开发的经验。 I will generally choose PHP for Web development because of: 通常会选择PHP进行Web开发,因为:

  • quicker to test changes in development (ie no build/deploy steps and Java hot-deploy has serious limitations). 更快地测试开发中的更改(即没有构建/部署步骤,Java热部署有严重的限制)。 Words cannot express how freeing it is to test changes by saving the file you're working on and clicking reload on a browser vs running an Ant/Maven build process; 单词无法表达通过保存您正在处理的文件并在浏览器上单击重新加载来运行Ant / Maven构建过程来测试更改的自由度。
  • far fewer issues of memory/resource leakage; 更少的内存/资源泄漏问题;
  • extensive library of functions to do pretty much anything you want; 丰富的功能库,可以完成您想要的任何功能;
  • cheaper to host (at the low end). 寄宿便宜(在低端)。

I will use Java for some things, like anything that involves a lot of background processing and threading, which aren't PHP's strong points. 我会将Java用于某些事情,比如任何涉及大量后台处理和线程的东西,这些都不是PHP的优点。

You'll note that performance (or the lack thereof) doesn't even rate as a reason for or again. 你会注意到表现(或缺乏表现)甚至没有作为理由或再次表示。

Sorry if that doesn't answer your question, but such concerns over performance are a pointless distraction. 对不起,如果这不能回答你的问题,但对性能的这种担忧是毫无意义的分心。

The best way to answer performance questions is with a benchmark. 回答性能问题的最佳方法是使用基准测试。 Implement some simple page in both PHP and Java and then benchmark them using ab (Apache Benchmark). 在PHP和Java中实现一些简单的页面,然后使用ab(Apache Benchmark)对它们进行基准测试。

Having said that, I suspect Java will outperform PHP because of the nature of the 2 platforms. 话虽如此,我怀疑Java会因为2平台的性质而优于PHP。 Java is compiled to optimized bytecode (once) and then interpreted by a virtual machine. Java被编译为优化的字节码(一次),然后由虚拟机解释。 When Tomcat runs, the JVM loads the classes required for any given page and keeps them in memory so they're ready to go when an HTTP request hits the web server. 当Tomcat运行时,JVM会加载任何给定页面所需的类,并将它们保存在内存中,这样当HTTP请求到达Web服务器时它们就可以运行了。 Contrast that with PHP which reloads and re-interprets the code from scratch with each invocation by Apache. 与PHP相比,它通过Apache的每次调用从头开始重新加载和重新解释代码。 This is helped to a large degree by op-code caching, but still not to the level of what happens in the JVM. 这在很大程度上受到操作码缓存的帮助,但仍未达到JVM中发生的水平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM