昨天看到一个php7新特性的pdf,今天看到php.net上有php7的snapshot for win下载,于是下了一个试试。

开始用字符串和类实例化调用魔术方法试了下,php7性能是php6.6的3倍。后来看到 链接的一个测试,里面和其他语言比较,php性能垫底了。然后此处对比5.3和5.4 ,虽然有强化,但是还是很弱。下了他的测试代码,是一个找质数的,基本就是纯数字运算。测试完,发现PHP7的JIT果然不是盖的。贴下测试代码和结果吧。。。

D:devtest>D:devphp7php -v
PHP 7.0.0-dev (cli) (built: Apr 24 2015 15:06:00)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
D:devtest>D:devphpphp -v
PHP 5.6.6 (cli) (built: Feb 18 2015 16:02:19)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
D:devtest>D:devphpphp test.php
Test1 total cost:0.033001899719238
Test2 total cost:0.12400698661804
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Test3 total cost:63.071608066559
D:devtest>D:devphp7php test.php
Test1 total cost:0.010000944137573
Test2 total cost:0.04600191116333
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Test3 total cost:12.118693113327

其中php6.6使用内存500+m,而php7使用内存180+m。。。差别实在是非常大,这说明了JIT对于数字运算的性能提升是非常明显的,远超其他操作,就如基本操作提升大于实际项目表现一样。另外说明了php7里对解释器内存使用的优化效果也是很明显的。开始php6.6默认的内存上限120M总是不够用,会报错。

下面贴测试代码:

<?php
ini_set("memory_limit", 512*1024*1024);
$start = microtime(true);
$string = "";
for($i=0;$i < 100000;++$i){
$string .= "{$i}";
}
$end = microtime(true);
echo "Test1 total cost:".($end-$start)."n";
/**
* 
*/
class TestClass 
{
function __construct()
{
# code...
}
function __call($name,$args){
if($name == "ttt"){return 1+1;}
}
}
$start = microtime(true);
for($i=0;$i < 100000;++$i){
$obj = new TestClass();
$obj->ttt();
}
$end = microtime(true);
echo "Test2 total cost:".($end-$start)."n";
error_reporting(E_ALL);
ini_set('display_errors', '1');
function get_primes7($n) {
if ($n < 2) return array();
if ($n == 2) return array(2);
$s = range(3, $n, 2);
$mroot = sqrt($n);
$half = count($s);
$i = 0;
$m = 3;
while ($m <= $mroot) {
if ($s[$i]) {
$j = (int)(($m*$m - 3) / 2);
$s[$j] = 0;
while ($j < $half) {
$s[$j] = 0;
$j += $m;
}
}
$i = $i + 1;
$m = 2*$i + 3;
}
$res = array(2);
foreach ($s as $v) {
if ($v) {
$res[] = $v;
}
}
return $res;
}
$res = array();
$start = microtime(true);
for ($i = 1; $i <= 10; ++$i) {
$res = get_primes7(10000000);
print "Found ".count($res)." prime numbers.n";
}
$end = microtime(true);
echo "Test3 total cost:".($end-$start)."n";

Source: xundaoinfo