PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1):
1.bench.php 可在PHP源代码的 php-src/Zend 目录
2. micro_bench.php 也可以在 PHP 源代码发布的 php-src/Zend 目录中找到
3.同时使用Unix time 命令来计时
time php micro_bench.php
PHP5.5.9结果:empty_loop 0.124func() 0.424 0.300undef_func() 0.422 0.298int_func() 0.343 0.219$x = self::$x 0.277 0.153self::$x = 0 0.351 0.227isset(self::$x) 0.469 0.345empty(self::$x) 0.498 0.374$x = Foo::$x 0.286 0.162Foo::$x = 0 0.271 0.147isset(Foo::$x) 0.319 0.195empty(Foo::$x) 0.360 0.236self::f() 1.304 1.180Foo::f() 1.396 1.272$x = $this->x 0.392 0.268$this->x = 0 0.500 0.376$this->x += 2 0.434 0.310++$this->x 0.271 0.147--$this->x 0.221 0.097$this->x++ 0.279 0.155$this->x-- 0.244 0.120isset($this->x) 0.238 0.114empty($this->x) 0.240 0.116$this->f() 0.711 0.587$x = Foo::TEST 0.519 0.395new Foo() 0.886 0.762$x = TEST 0.204 0.080$x = $_GET 0.250 0.126$x = $GLOBALS['v'] 0.421 0.297$x = $hash['v'] 0.254 0.130$x = $str[0] 0.362 0.238$x = $a ?: null 0.248 0.124$x = $f ?: tmp 0.331 0.207$x = $f ? $f : $a 0.255 0.131$x = $f ? $f : tmp 0.333 0.209------------------------Total 14.441real 0m14.498suser 0m12.110ssys 0m0.024s
PHP7.2.1结果:empty_loop 0.066func() 0.302 0.236undef_func() 0.505 0.439int_func() 0.258 0.192$x = self::$x 0.269 0.203self::$x = 0 0.223 0.157isset(self::$x) 0.212 0.146empty(self::$x) 0.213 0.147$x = Foo::$x 0.220 0.154Foo::$x = 0 0.168 0.102isset(Foo::$x) 0.100 0.034empty(Foo::$x) 0.116 0.050self::f() 0.183 0.117Foo::f() 0.120 0.054$x = $this->x 0.099 0.033$this->x = 0 0.065 0.000$this->x += 2 0.125 0.059++$this->x 0.100 0.034--$this->x 0.082 0.017$this->x++ 0.120 0.054$this->x-- 0.160 0.094isset($this->x) 0.173 0.107empty($this->x) 0.163 0.097$this->f() 0.135 0.069$x = Foo::TEST 0.125 0.059new Foo() 0.337 0.271$x = TEST 0.074 0.008$x = $_GET 0.121 0.055$x = $GLOBALS['v'] 0.185 0.119$x = $hash['v'] 0.153 0.087$x = $str[0] 0.119 0.053$x = $a ?: null 0.098 0.032$x = $f ?: tmp 0.107 0.041$x = $f ? $f : $a 0.101 0.035$x = $f ? $f : tmp 0.095 0.029------------------------Total 5.694real 0m5.756suser 0m4.521ssys 0m0.028s
性能提升接近三倍
micro_bench.php 测试脚本<?phpfunction hallo() {}function simpleucall($n) { for ($i = 0; $i < $n; $i++) hallo();}function simpleudcall($n) { for ($i = 0; $i < $n; $i++) hallo2();}function hallo2() {}function simpleicall($n) { for ($i = 0; $i < $n; $i++) func_num_args();}class Foo { static $a = 0; public $b = 0; const TEST = 0; static function read_static($n) { for ($i = 0; $i < $n; ++$i) { $x = self::$a; } } static function write_static($n) { for ($i = 0; $i < $n; ++$i) { self::$a = 0; } } static function isset_static($n) { for ($i = 0; $i < $n; ++$i) { $x = isset(self::$a); } } static function empty_static($n) { for ($i = 0; $i < $n; ++$i) { $x = empty(self::$a); } } static function f() { } static function call_static($n) { for ($i = 0; $i < $n; ++$i) { self::f(); } } function read_prop($n) { for ($i = 0; $i < $n; ++$i) { $x = $this->b; } } function write_prop($n) { for ($i = 0; $i b = 0; } } function assign_add_prop($n) { for ($i = 0; $i b += 2; } } function pre_inc_prop($n) { for ($i = 0; $i b; } } function pre_dec_prop($n) { for ($i = 0; $i b; } } function post_inc_prop($n) { for ($i = 0; $i b++; } } function post_dec_prop($n) { for ($i = 0; $i b--; } } function isset_prop($n) { for ($i = 0; $i b); } } function empty_prop($n) { for ($i = 0; $i b); } } function g() { } function call($n) { for ($i = 0; $i g(); } } function read_const($n) { for ($i = 0; $i 0); for ($i = 0; $i read_prop(N);$t = end_test($t, '$x = $this->x', $overhead);$x->write_prop(N);$t = end_test($t, '$this->x = 0', $overhead);$x->assign_add_prop(N);$t = end_test($t, '$this->x += 2', $overhead);$x->pre_inc_prop(N);$t = end_test($t, '++$this->x', $overhead);$x->pre_dec_prop(N);$t = end_test($t, '--$this->x', $overhead);$x->post_inc_prop(N);$t = end_test($t, '$this->x++', $overhead);$x->post_dec_prop(N);$t = end_test($t, '$this->x--', $overhead);$x->isset_prop(N);$t = end_test($t, 'isset($this->x)', $overhead);$x->empty_prop(N);$t = end_test($t, 'empty($this->x)', $overhead);$x->call(N);$t = end_test($t, '$this->f()', $overhead);$x->read_const(N);$t = end_test($t, '$x = Foo::TEST', $overhead);create_object(N);$t = end_test($t, 'new Foo()', $overhead);read_const(N);$t = end_test($t, '$x = TEST', $overhead);read_auto_global(N);$t = end_test($t, '$x = $_GET', $overhead);read_global_var(N);$t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead);read_hash(N);$t = end_test($t, '$x = $hash[\'v\']', $overhead);read_str_offset(N);$t = end_test($t, '$x = $str[0]', $overhead);issetor(N);$t = end_test($t, '$x = $a ?: null', $overhead);issetor2(N);$t = end_test($t, '$x = $f ?: tmp', $overhead);ternary(N);$t = end_test($t, '$x = $f ? $f : $a', $overhead);ternary2(N);$t = end_test($t, '$x = $f ? $f : tmp', $overhead);total($t0, "Total");