PHP 与 JavaScript 的速度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8866738/
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-24 07:51:18  来源:igfitidea点击:

Speed of PHP vs JavaScript?

phpjavascriptperformance

提问by Azmisov

I am writing a script to tokenize file contents. I've written a tokenizer in JavaScript, but it gets pretty slow with large files (5+ seconds). Since I'm retrieving the files from the server anyways, I am considering rewriting it in PHP to shave off a few seconds. I've read that the speed difference between PHP and JavaScript is negligible, but if the files are 1mb or bigger, a small speed difference could add up. I found this benchmarkonline, but I'm not sure if the info is up-to-date (with all the JS engine optimizations from the past couple years).

我正在编写一个脚本来标记文件内容。我已经用 JavaScript 编写了一个标记器,但是对于大文件(5 秒以上),它变得非常慢。由于无论如何我都是从服务器检索文件,我正在考虑用 PHP 重写它以减少几秒钟的时间。我读过 PHP 和 JavaScript 之间的速度差异可以忽略不计,但如果文件为 1mb 或更大,则可能会增加一个小的速度差异。我在网上找到了这个基准测试,但我不确定信息是否是最新的(包括过去几年的所有 JS 引擎优化)。

Porting over all my code would take some work, so if I can help it, I won't use PHP. But if its going to be much faster (25-30%), it would be worth it.

移植我所有的代码需要一些工作,所以如果我能帮上忙,我不会使用 PHP。但如果它要快得多(25-30%),那将是值得的。

采纳答案by Authman Apatira

Well, like you alluded, JavaScript is executed on the client side, so it would depend on the client. For example, a client on an iPAD wouldn't be able to run those Quake3 HTML5s as smoothly as a client running on a gaming machine. PHP on the other hand runs on the server.

好吧,就像你提到的,JavaScript 在客户端执行,所以它取决于客户端。例如,iPAD 上的客户端无法像在游戏机上运行的客户端一样流畅地运行这些 Quake3 HTML5。另一方面,PHP 在服务器上运行。

In general, JS is faster considerably than PHP though (on the same hardware);

一般来说,JS 比 PHP 快得多(在相同的硬件上);

Here are some [hard figures][1] of tests between the two, to back up my assertion. [JS (query) vs PHP][2] Performance.

以下是两者之间的一些[硬数据][1] 测试,以支持我的断言。[JS(查询)与 PHP][2] 性能。

[1]: https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/php-node.html[2]: jQuery vs. PHP - Performance Comparison

[1]: https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/php-node.html[2]: jQuery vs. PHP - 性能比较

回答by Zauker

I just converted a script from PHP to Javascript and I have greatly increased performance.

我刚刚将脚本从 PHP 转换为 Javascript,并且性能大大提高。

Javascript seems much faster in mathematical calculations. I have personally witnessed the performance improvement, my PHP procedure in some cases also employed a few seconds of running, while the same procedure written in javascript takes at most a few milliseconds.

Javascript 在数学计算中似乎要快得多。我亲眼目睹了性能的提升,我的 PHP 程序在某些情况下也运行了几秒钟,而用 javascript 编写的相同程序最多需要几毫秒。

Although Javascript to be known as client-side language I run my code on the server side thanks to Node.js

尽管 Javascript 被称为客户端语言,但由于 Node.js,我在服务器端运行我的代码

Here you can see the benchmark between Javascript V8 and PHP

在这里你可以看到 Javascript V8 和 PHP 之间的基准测试

https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/php-node.html

https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/php-node.html

回答by Ukuser32

Its worth saying that another consideration for using PHP is the ability to cache server side. I'm looking at this considering if I should move some of the functions from JS to PHP because if I then cache the files server side there is no further calculations involved at all! I know this ticket is old but this may be another consideration for future readers.

值得一提的是,使用 PHP 的另一个考虑因素是缓存服务器端的能力。我正在考虑是否应该将某些功能从 JS 移至 PHP,因为如果我然后缓存文件服务器端,则根本不涉及进一步的计算!我知道这张票很旧,但这可能是未来读者的另一个考虑因素。