JavaScript 代码几乎是 1:1 编译的,因此没有太多实质内容的 10^9 次循环会以 CPU 允许的速度运行(在这里:使用旧版 Node v0.10.25 运行时间为 2.14 秒)。
另一方面,PHP 做了很多工作,特别是 Zend 引擎。如果使用
VLD 转储你的小程序的操作码,你会得到以下结果(php 7.0.5):
$ php -d vld.active=1 -d vld.execute=0 -f benchmark.php
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 14
Branch analysis from position: 14
Jump found. Position 1 = 16, Position 2 = 4
Branch analysis from position: 16
Jump found. Position 1 = -2
Branch analysis from position: 4
Jump found. Position 1 = 10
Branch analysis from position: 10
Jump found. Position 1 = 12, Position 2 = 6
Branch analysis from position: 12
Jump found. Position 1 = 16, Position 2 = 4
Branch analysis from position: 16
Branch analysis from position: 4
Branch analysis from position: 6
Jump found. Position 1 = 12, Position 2 = 6
Branch analysis from position: 12
Branch analysis from position: 6
filename: benchmark.php
function name: (null)
number of ops: 21
compiled vars: !0 = $a, !1 = $b, !2 = $j, !3 = $i
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > ASSIGN !0, 3.14159
3 1 ASSIGN !1, 2.718
5 2 ASSIGN !2, 0
3 > JMP ->14
6 4 > ASSIGN !3, 0
5 > JMP ->10
7 6 > ADD ~8 !0, !1
7 ASSIGN !0, ~8
6 8 POST_INC ~10 !3
9 FREE ~10
10 > IS_SMALLER ~11 !3, 100000000
11 > JMPNZ ~11, ->6
5 12 > POST_INC ~12 !2
13 FREE ~12
14 > IS_SMALLER ~13 !2, 10
15 > JMPNZ ~13, ->4
10 16 > ROPE_INIT 3 ~15 'a+%3D+'
17 ROPE_ADD 1 ~15 ~15, !0
18 ROPE_END 2 ~14 ~15, '%0A'
19 ECHO ~14
20 > RETURN 1
branch: # 0; line: 2- 5; sop: 0; eop: 3; out1: 14
branch: # 4; line: 6- 6; sop: 4; eop: 5; out1: 10
branch: # 6; line: 7- 6; sop: 6; eop: 9; out1: 10
branch: # 10; line: 6- 6; sop: 10; eop: 11; out1: 12; out2: 6
branch: # 12; line: 5- 5; sop: 12; eop: 13; out1: 14
branch: # 14; line: 5- 5; sop: 14; eop: 15; out1: 16; out2: 4
branch: # 16; line: 10- 10; sop: 16; eop: 20; out1: -2
path #1: 0, 14, 16,
path #2: 0, 14, 4, 10, 12, 14, 16,
path #3: 0, 14, 4, 10, 6, 10, 12, 14, 16,
你两个版本的区别是:
1,10c1,10
< branch: # 0; line: 2- 5; sop: 0; eop: 3; out1: 14
< branch: # 4; line: 6- 6; sop: 4; eop: 5; out1: 10
< branch: # 6; line: 7- 6; sop: 6; eop: 9; out1: 10
< branch: # 10; line: 6- 6; sop: 10; eop: 11; out1: 12; out2: 6
< branch: # 12; line: 5- 5; sop: 12; eop: 13; out1: 14
< branch: # 14; line: 5- 5; sop: 14; eop: 15; out1: 16; out2: 4
< branch: # 16; line: 10- 10; sop: 16; eop: 20; out1: -2
< path #1: 0, 14, 16,
< path #2: 0, 14, 4, 10, 12, 14, 16,
< path #3: 0, 14, 4, 10, 6, 10, 12, 14, 16,
---
> branch: # 0; line: 2- 5; sop: 0; eop: 3; out1: 13
> branch: # 4; line: 6- 6; sop: 4; eop: 5; out1: 9
> branch: # 6; line: 7- 6; sop: 6; eop: 8; out1: 9
> branch: # 9; line: 6- 6; sop: 9; eop: 10; out1: 11; out2: 6
> branch: # 11; line: 5- 5; sop: 11; eop: 12; out1: 13
> branch: # 13; line: 5- 5; sop: 13; eop: 14; out1: 15; out2: 4
> branch: # 15; line: 10- 10; sop: 15; eop: 19; out1: -2
> path #1: 0, 13, 15,
> path #2: 0, 13, 4, 9, 11, 13, 15,
> path #3: 0, 13, 4, 9, 6, 9, 11, 13, 15,
它(`$a += $b`)只是使用了不同的,稍微慢一点的路径。是的,慢:我的测试结果为 `$a = $a + $b` 花费了 17 秒,而 `$a += $b` 则花费了 20 秒。虽然差别不大,但是很显著。和 JavaScript 相比也没有太大的区别。
对于您所使用的 PHP 版本来说,两分钟的时间相当长,即使是旧的 PHP-5 在这里也只需要 40 秒钟。我在变更日志中找不到任何东西,但如果可能的话,您可以尝试更新版本。或者,如果您自己编译,可以尝试不同的优化,因为 MAC 仍然与“普通” PC 不同。
a = a + b * 1000000000;
。 - Barmar