PHP5.6.40とPHP7.0.1及び7.4.4性能比較試験(隙間がないないコントラスト)

1、インストールパッケージをダウンロードしてください

cd ~
wget http://php.net/distributions/php-7.0.1.tar.gz
tar -zxvf php-7.0.1.tar.gz
wget http://php.net/distributions/php-5.6.40.tar.gz
tar -zxvf php-5.6.40.tar.gz
wget http://php.net/distributions/php-7.4.4.tar.gz
tar -zxvf php-7.4.4.tar.gz

2、コンパイルしてインストールします

cd ~/php-5.6.40
./configure --prefix=/usr/local/php-5.6.40 --enable-fpm
make
make install
cd ~/php-7.0.1
./configure --prefix=/usr/local/php-7.0.1 --enable-fpm
make
nake install
./configure --prefix=/usr/local/php-7.4.4 --enable-fpm
make
nake install

3、テストを開始

ソースディレクトリが提供するテストコードを使用してテストし、ソースツリービューのPHPコードに行く、記事は最終的にテストコードを掲載します

/usr/local/php-7.4.4/bin/php ~/php-7.0.1/Zend/bench.php
/usr/local/php-7.0.1/bin/php ~/php-7.0.1/Zend/bench.php
/usr/local/php-5.6.40/bin/php ~/php-7.0.1/Zend/bench.php

結果は以下の通りであります:

7.4.4:

7.0.1:

5.6.40:

三のギャップ、PHP5実行時間の上側からリフトに関してより明白PHP7を見ることができます。

より詳細なテストコードがソースディレクトリ内のZend / micro_bench.phpでもあります:

7.4.4:

7.0.1:

5.6.40:

4、要約

PHP5に比べPHP7は二倍以上にアップグレードしてください。

テストコード:

bench.php

<?php
if (function_exists("date_default_timezone_set")) {
	date_default_timezone_set("UTC");
}

function simple() {
  $a = 0;
  for ($i = 0; $i < 1000000; $i++) 
    $a++;

  $thisisanotherlongname = 0;
  for ($thisisalongname = 0; $thisisalongname < 1000000; $thisisalongname++) 
    $thisisanotherlongname++;
}

/****/

function simplecall() {
  for ($i = 0; $i < 1000000; $i++) 
    strlen("hallo");
}

/****/

function hallo($a) {
}

function simpleucall() {
  for ($i = 0; $i < 1000000; $i++) 
    hallo("hallo");
}

/****/

function simpleudcall() {
  for ($i = 0; $i < 1000000; $i++) 
    hallo2("hallo");
}

function hallo2($a) {
}

/****/

function mandel() {
  $w1=50;
  $h1=150;
  $recen=-.45;
  $imcen=0.0;
  $r=0.7;
  $s=0;  $rec=0;  $imc=0;  $re=0;  $im=0;  $re2=0;  $im2=0;
  $x=0;  $y=0;  $w2=0;  $h2=0;  $color=0;
  $s=2*$r/$w1;
  $w2=40;
  $h2=12;
  for ($y=0 ; $y<=$w1; $y=$y+1) {
    $imc=$s*($y-$h2)+$imcen;
    for ($x=0 ; $x<=$h1; $x=$x+1) {
      $rec=$s*($x-$w2)+$recen;
      $re=$rec;
      $im=$imc;
      $color=1000;
      $re2=$re*$re;
      $im2=$im*$im;
      while( ((($re2+$im2)<1000000) && $color>0)) {
        $im=$re*$im*2+$imc;
        $re=$re2-$im2+$rec;
        $re2=$re*$re;
        $im2=$im*$im;
        $color=$color-1;
      }
      if ( $color==0 ) {
        print "_";
      } else {
        print "#";
      }
    }
    print "<br>";
    flush();
  }
}

/****/

function mandel2() {
  $b = " .:,;!/>)|&IH%*#";
  //float r, i, z, Z, t, c, C;
  for ($y=30; printf("\n"), $C = $y*0.1 - 1.5, $y--;){
    for ($x=0; $c = $x*0.04 - 2, $z=0, $Z=0, $x++ < 75;){
      for ($r=$c, $i=$C, $k=0; $t = $z*$z - $Z*$Z + $r, $Z = 2*$z*$Z + $i, $z=$t, $k<5000; $k++)
        if ($z*$z + $Z*$Z > 500000) break;
      echo $b[$k%16];
    }
  }
}

/****/

function Ack($m, $n){
  if($m == 0) return $n+1;
  if($n == 0) return Ack($m-1, 1);
  return Ack($m - 1, Ack($m, ($n - 1)));
}

function ackermann($n) {
  $r = Ack(3,$n);
  print "Ack(3,$n): $r\n";
}

/****/

function ary($n) {
  for ($i=0; $i<$n; $i++) {
    $X[$i] = $i;
  }
  for ($i=$n-1; $i>=0; $i--) {
    $Y[$i] = $X[$i];
  }
  $last = $n-1;
  print "$Y[$last]\n";
}

/****/

function ary2($n) {
  for ($i=0; $i<$n;) {
    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;

    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;
    $X[$i] = $i; ++$i;
  }
  for ($i=$n-1; $i>=0;) {
    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;

    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;
    $Y[$i] = $X[$i]; --$i;
  }
  $last = $n-1;
  print "$Y[$last]\n";
}

/****/

function ary3($n) {
  for ($i=0; $i<$n; $i++) {
    $X[$i] = $i + 1;
    $Y[$i] = 0;
  }
  for ($k=0; $k<1000; $k++) {
    for ($i=$n-1; $i>=0; $i--) {
      $Y[$i] += $X[$i];
    }
  }
  $last = $n-1;
  print "$Y[0] $Y[$last]\n";
}

/****/

function fibo_r($n){
    return(($n < 2) ? 1 : fibo_r($n - 2) + fibo_r($n - 1));
}

function fibo($n) {
  $r = fibo_r($n);
  print "$r\n";
}

/****/

function hash1($n) {
  for ($i = 1; $i <= $n; $i++) {
    $X[dechex($i)] = $i;
  }
  $c = 0;
  for ($i = $n; $i > 0; $i--) {
    if ($X[dechex($i)]) { $c++; }
  }
  print "$c\n";
}

/****/

function hash2($n) {
  for ($i = 0; $i < $n; $i++) {
    $hash1["foo_$i"] = $i;
    $hash2["foo_$i"] = 0;
  }
  for ($i = $n; $i > 0; $i--) {
    foreach($hash1 as $key => $value) $hash2[$key] += $value;
  }
  $first = "foo_0";
  $last  = "foo_".($n-1);
  print "$hash1[$first] $hash1[$last] $hash2[$first] $hash2[$last]\n";
}

/****/

function gen_random ($n) {
    global $LAST;
    return( ($n * ($LAST = ($LAST * IA + IC) % IM)) / IM );
}

function heapsort_r($n, &$ra) {
    $l = ($n >> 1) + 1;
    $ir = $n;

    while (1) {
	if ($l > 1) {
	    $rra = $ra[--$l];
	} else {
	    $rra = $ra[$ir];
	    $ra[$ir] = $ra[1];
	    if (--$ir == 1) {
		$ra[1] = $rra;
		return;
	    }
	}
	$i = $l;
	$j = $l << 1;
	while ($j <= $ir) {
	    if (($j < $ir) && ($ra[$j] < $ra[$j+1])) {
		$j++;
	    }
	    if ($rra < $ra[$j]) {
		$ra[$i] = $ra[$j];
		$j += ($i = $j);
	    } else {
		$j = $ir + 1;
	    }
	}
	$ra[$i] = $rra;
    }
}

function heapsort($N) {
  global $LAST;

  define("IM", 139968);
  define("IA", 3877);
  define("IC", 29573);

  $LAST = 42;
  for ($i=1; $i<=$N; $i++) {
    $ary[$i] = gen_random(1);
  }
  heapsort_r($N, $ary);
  printf("%.10f\n", $ary[$N]);
}

/****/

function mkmatrix ($rows, $cols) {
    $count = 1;
    $mx = array();
    for ($i=0; $i<$rows; $i++) {
	for ($j=0; $j<$cols; $j++) {
	    $mx[$i][$j] = $count++;
	}
    }
    return($mx);
}

function mmult ($rows, $cols, $m1, $m2) {
    $m3 = array();
    for ($i=0; $i<$rows; $i++) {
	for ($j=0; $j<$cols; $j++) {
	    $x = 0;
	    for ($k=0; $k<$cols; $k++) {
		$x += $m1[$i][$k] * $m2[$k][$j];
	    }
	    $m3[$i][$j] = $x;
	}
    }
    return($m3);
}

function matrix($n) {
  $SIZE = 30;
  $m1 = mkmatrix($SIZE, $SIZE);
  $m2 = mkmatrix($SIZE, $SIZE);
  while ($n--) {
    $mm = mmult($SIZE, $SIZE, $m1, $m2);
  }
  print "{$mm[0][0]} {$mm[2][3]} {$mm[3][2]} {$mm[4][4]}\n";
}

/****/

function nestedloop($n) {
  $x = 0;
  for ($a=0; $a<$n; $a++)
    for ($b=0; $b<$n; $b++)
      for ($c=0; $c<$n; $c++)
        for ($d=0; $d<$n; $d++)
          for ($e=0; $e<$n; $e++)
            for ($f=0; $f<$n; $f++)
             $x++;
  print "$x\n";
}

/****/

function sieve($n) {
  $count = 0;
  while ($n-- > 0) {
    $count = 0;
    $flags = range (0,8192);
    for ($i=2; $i<8193; $i++) {
      if ($flags[$i] > 0) {
        for ($k=$i+$i; $k <= 8192; $k+=$i) {
          $flags[$k] = 0;
        }
        $count++;
      }
    }
  }
  print "Count: $count\n";
}

/****/

function strcat($n) {
  $str = "";
  while ($n-- > 0) {
    $str .= "hello\n";
  }
  $len = strlen($str);
  print "$len\n";
}

/*****/

function getmicrotime()
{
  $t = gettimeofday();
  return ($t['sec'] + $t['usec'] / 1000000);
}

function start_test()
{
	ob_start();
  return getmicrotime();
}

function end_test($start, $name)
{
  global $total;
  $end = getmicrotime();
  ob_end_clean();
  $total += $end-$start;
  $num = number_format($end-$start,3);
  $pad = str_repeat(" ", 24-strlen($name)-strlen($num));

  echo $name.$pad.$num."\n";
	ob_start();
  return getmicrotime();
}

function total()
{
  global $total;
  $pad = str_repeat("-", 24);
  echo $pad."\n";
  $num = number_format($total,3);
  $pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
  echo "Total".$pad.$num."\n";
}

$t0 = $t = start_test();
simple();
$t = end_test($t, "simple");
simplecall();
$t = end_test($t, "simplecall");
simpleucall();
$t = end_test($t, "simpleucall");
simpleudcall();
$t = end_test($t, "simpleudcall");
mandel();
$t = end_test($t, "mandel");
mandel2();
$t = end_test($t, "mandel2");
ackermann(7);
$t = end_test($t, "ackermann(7)");
ary(50000);
$t = end_test($t, "ary(50000)");
ary2(50000);
$t = end_test($t, "ary2(50000)");
ary3(2000);
$t = end_test($t, "ary3(2000)");
fibo(30);
$t = end_test($t, "fibo(30)");
hash1(50000);
$t = end_test($t, "hash1(50000)");
hash2(500);
$t = end_test($t, "hash2(500)");
heapsort(20000);
$t = end_test($t, "heapsort(20000)");
matrix(20);
$t = end_test($t, "matrix(20)");
nestedloop(12);
$t = end_test($t, "nestedloop(12)");
sieve(30);
$t = end_test($t, "sieve(30)");
strcat(200000);
$t = end_test($t, "strcat(200000)");
total($t0, "Total");
?>

micro_bench.php:

<?php

function 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 < $n; ++$i) {
			$this->b = 0;
		}
	}

	function assign_add_prop($n) {
		for ($i = 0; $i < $n; ++$i) {
			$this->b += 2;
		}
	}

	function pre_inc_prop($n) {
		for ($i = 0; $i < $n; ++$i) {
			++$this->b;
		}
	}

	function pre_dec_prop($n) {
		for ($i = 0; $i < $n; ++$i) {
			--$this->b;
		}
	}

	function post_inc_prop($n) {
		for ($i = 0; $i < $n; ++$i) {
			$this->b++;
		}
	}

	function post_dec_prop($n) {
		for ($i = 0; $i < $n; ++$i) {
			$this->b--;
		}
	}

	function isset_prop($n) {
		for ($i = 0; $i < $n; ++$i) {
			$x = isset($this->b);
		}
	}

	function empty_prop($n) {
		for ($i = 0; $i < $n; ++$i) {
			$x = empty($this->b);
		}
	}

	function g() {
	}

	function call($n) {
		for ($i = 0; $i < $n; ++$i) {
			$this->g();
		}
	}

	function read_const($n) {
		for ($i = 0; $i < $n; ++$i) {
			$x = $this::TEST;
		}
	}

}

function read_static($n) {
	for ($i = 0; $i < $n; ++$i) {
		$x = Foo::$a;
	}
}

function write_static($n) {
	for ($i = 0; $i < $n; ++$i) {
		Foo::$a = 0;
	}
}

function isset_static($n) {
	for ($i = 0; $i < $n; ++$i) {
		$x = isset(Foo::$a);
	}
}

function empty_static($n) {
	for ($i = 0; $i < $n; ++$i) {
		$x = empty(Foo::$a);
	}
}

function call_static($n) {
	for ($i = 0; $i < $n; ++$i) {
		Foo::f();
	}
}

function create_object($n) {
	for ($i = 0; $i < $n; ++$i) {
		$x = new Foo();
	}
}

define('TEST', null);

function read_const($n) {
	for ($i = 0; $i < $n; ++$i) {
		$x = TEST;
	}
}

function read_auto_global($n) {
	for ($i = 0; $i < $n; ++$i) {
		$x = $_GET;
	}
}

$g_var = 0;

function read_global_var($n) {
	for ($i = 0; $i < $n; ++$i) {
		$x = $GLOBALS['g_var'];
	}
}

function read_hash($n) {
	$hash = array('test' => 0);
	for ($i = 0; $i < $n; ++$i) {
		$x = $hash['test'];
	}
}

function read_str_offset($n) {
	$str = "test";
	for ($i = 0; $i < $n; ++$i) {
		$x = $str[1];
	}
}

function issetor($n) {
	$val = array(0,1,2,3,4,5,6,7,8,9);
	for ($i = 0; $i < $n; ++$i) {
		$x = $val ?: null;
	}
}

function issetor2($n) {
	$f = false; $j = 0;
	for ($i = 0; $i < $n; ++$i) {
		$x = $f ?: $j + 1;
	}
}

function ternary($n) {
	$val = array(0,1,2,3,4,5,6,7,8,9);
	$f = false;
	for ($i = 0; $i < $n; ++$i) {
		$x = $f ? null : $val;
	}
}

function ternary2($n) {
	$f = false; $j = 0;
	for ($i = 0; $i < $n; ++$i) {
		$x = $f ? $f : $j + 1;
	}
}

/*****/

function empty_loop($n) {
	for ($i = 0; $i < $n; ++$i) {
	}
}

function getmicrotime()
{
  $t = gettimeofday();
  return ($t['sec'] + $t['usec'] / 1000000);
}

function start_test()
{
  ob_start();
  return getmicrotime();
}

function end_test($start, $name, $overhead = null)
{
  global $total;
  global $last_time;
  $end = getmicrotime();
  ob_end_clean();
  $last_time = $end-$start;
  $total += $last_time;
  $num = number_format($last_time,3);
  $pad = str_repeat(" ", 24-strlen($name)-strlen($num));
  if (is_null($overhead)) {
    echo $name.$pad.$num."\n";
  } else {
    $num2 = number_format($last_time - $overhead,3);
    echo $name.$pad.$num."    ".$num2."\n";
  }
  ob_start();
  return getmicrotime();
}

function total()
{
  global $total;
  $pad = str_repeat("-", 24);
  echo $pad."\n";
  $num = number_format($total,3);
  $pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
  echo "Total".$pad.$num."\n";
}

const N = 5000000;

$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
simpleucall(N);
$t = end_test($t, 'func()', $overhead);
simpleudcall(N);
$t = end_test($t, 'undef_func()', $overhead);
simpleicall(N);
$t = end_test($t, 'int_func()', $overhead);
Foo::read_static(N);
$t = end_test($t, '$x = self::$x', $overhead);
Foo::write_static(N);
$t = end_test($t, 'self::$x = 0', $overhead);
Foo::isset_static(N);
$t = end_test($t, 'isset(self::$x)', $overhead);
Foo::empty_static(N);
$t = end_test($t, 'empty(self::$x)', $overhead);
read_static(N);
$t = end_test($t, '$x = Foo::$x', $overhead);
write_static(N);
$t = end_test($t, 'Foo::$x = 0', $overhead);
isset_static(N);
$t = end_test($t, 'isset(Foo::$x)', $overhead);
empty_static(N);
$t = end_test($t, 'empty(Foo::$x)', $overhead);
Foo::call_static(N);
$t = end_test($t, 'self::f()', $overhead);
call_static(N);
$t = end_test($t, 'Foo::f()', $overhead);
$x = new Foo();
$x->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");

 

公開された109元の記事 ウォンの賞賛101 ビュー360 000 +

おすすめ

転載: blog.csdn.net/Alen_xiaoxin/article/details/105176835