search
Homephp教程php手册提升PHP性能之改变Zend引擎分发方式

 

  从PHP5.1开始,PHP提供了用户对Zend VM执行分发方式的选择接口.

  之前的文章中, 我也提过这方面的内容, Zend虚拟机在执行的时候, 对于编译生成的op_array中的每一条opline的opcode都会分发到相应的处理器(zend_vm_def.h定义)执行, 而按照分发的方式不同, 分发过程可以分为CALL, SWITCH, 和GOTO三种类型.

  默认是CALL方式, 也就是所有的opcode处理器都定义为函数, 然后虚拟机调用. 这种方式是传统的方式, 也一般被认为是最稳定的方式.

  SWITCH方式和GOTO方式则和其命名的意义相同, 分别通过switch和goto来分发opcode到对应的处理逻辑(段).

  官方给出的描述是:

  CALL – Uses function handlers for opcodes

  SWITCH – Uses switch() statement for opcode dispatch

  GOTO – Uses goto for opcode dispatch (threaded opcodes architecture)

  GOTO is usually (depends on CPU and compiler) faster than SWITCH, which

  tends to be slightly faster than CALL.

  CALL is default because it doesn’t take very long to compile as opposed

  to the other two and in general the speed is quite close to the others.

  那么如果使用GOTO方式, 效率上到底能提高多少呢?

  今天我就分别使用各种方式来测试一番, 测试脚本bench.php.

  第一点被证明的就是, 官方说的GOTO方式编译耗时显著高于其他俩种方式, 我一开始在虚拟机上编译, 每次都Hangup(囧), 最后只好换了个强劲点的物理机, 大约3分钟后, 编译成功..

  测试环境:

 

  PHP 5.3.0 Linux

  AMD Opteron(tm) Processor 270(2G) X 4 6G Memory

 

  编译参数:

 

  ./configure --with-zend-vm=CALL/GOTO/SWITCH

 

  测试结果如下(都是三次取中值):

  CALL方式:

 

  laruence@dev01.tc$ sapi/cli/php bench.php

  simple 0.358

  simplecall 0.418

  simpleucall 0.405

  simpleudcall 0.424

  mandel 1.011

  mandel2 1.238

  ackermann(7) 0.375

  ary(50000) 0.083

  ary2(50000) 0.075

  ary3(2000) 0.561

  fibo(30) 1.156

  hash1(50000) 0.114

  hash2(500) 0.091

  heapsort(20000) 0.270

  matrix(20) 0.276

  nestedloop(12) 0.599

  sieve(30) 0.350

  strcat(200000) 0.039

  ------------------------

  Total 7.844

 

  SWITCH方式:

 

  laruence@dev01.tc$ sapi/cli/php bench.php

  simple 0.393

  simplecall 0.414

  simpleucall 0.424

  simpleudcall 0.445

  mandel 1.007

  mandel2 1.254

  ackermann(7) 0.392

  ary(50000) 0.084

  ary2(50000) 0.073

  ary3(2000) 0.593

  fibo(30) 1.185

  hash1(50000) 0.120

  hash2(500) 0.092

  heapsort(20000) 0.285

  matrix(20) 0.295

  nestedloop(12) 0.678

  sieve(30) 0.359

  strcat(200000) 0.042

  ------------------------

  Total 8.138

 

  GOTO方式 :

 

  laruence@dev01.tc$ sapi/cli/php bench.php

  simple 0.306

  simplecall 0.373

  simpleucall 0.369

  simpleudcall 0.385

  mandel 0.879

  mandel2 1.132

  ackermann(7) 0.356

  ary(50000) 0.081

  ary2(50000) 0.073

  ary3(2000) 0.525

  fibo(30) 1.043

  hash1(50000) 0.111

  hash2(500) 0.088

  heapsort(20000) 0.247

  matrix(20) 0.247

  nestedloop(12) 0.519

  sieve(30) 0.331

  strcat(200000) 0.037

  ------------------------

  Total 7.103

 

  可见, GOTO方式最快, SWITCH方式最慢.和官方的描述稍有不符.

  GOTO方式比其默认的CALL方式, 性能提升还是比较明显的.

  所以, 如果你希望让PHP发挥到机制, 改变Zend VM的分发方式, 也可以做为一个考虑因素.

  附:

  使用GOTO方式的configure选项:

 

  --with-zend-vm=GOTO

 

  也可以在Zend目录下使用:

 

  php zend_vm_gen.php --with-vm-kind=[CALLGOTOSWITH]

 

  测试脚本bench.php

 

  

  /**

  * PHP Perf Bench Test Script

  */

  function simple() {

  $a = 0;

  for ($i = 0; $i

  $a++;

  $thisisanotherlongname = 0;

  for ($thisisalongname = 0; $thisisalongname

  $thisisanotherlongname++;

  }

  /****/

  function simplecall() {

  for ($i = 0; $i

  strlen("hallo");

  }

  /****/

  function hallo($a) {

  }

  function simpleucall() {

  for ($i = 0; $i

  hallo("hallo");

  }

  /****/

  function simpleudcall() {

  for ($i = 0; $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

  $imc=$s*($y-$h2)+$imcen;

  for ($x=0 ; $x

  $rec=$s*($x-$w2)+$recen;

  $re=$rec;

  $im=$imc;

  $color=1000;

  $re2=$re*$re;

  $im2=$im*$im;

  while( ((($re2+$im2)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 "
";

  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++

  for ($r=$c, $i=$C, $k=0; $t = $z*$z - $Z*$Z + $r, $Z = 2*$z*$Z + $i, $z=$t, $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

  $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

  $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

  $X[$i] = $i + 1;

  $Y[$i] = 0;

  }

  for ($k=0; $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

  }

  function fibo($n) {

  $r = fibo_r($n);

  print "$r\n";

  }

  /****/

  function hash1($n) {

  for ($i = 1; $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

  $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

  while ($j

  if (($j

  $j++;

  }

  if ($rra

  $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

  $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

  for ($j=0; $j

  $mx[$i][$j] = $count++;

  }

  }

  return($mx);

  }

  function mmult ($rows, $cols, $m1, $m2) {

  $m3 = array();

  for ($i=0; $i

  for ($j=0; $j

  $x = 0;

  for ($k=0; $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

  for ($b=0; $b

  for ($c=0; $c

  for ($d=0; $d

  for ($e=0; $e

  for ($f=0; $f

  $x++;

  print "$x\n";

  }

  /****/

  function sieve($n) {

  $count = 0;

  while ($n-- > 0) {

  $count = 0;

  $flags = range (0,8192);

  for ($i=2; $i

  if ($flags[$i] > 0) {

  for ($k=$i+$i; $k

  $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");

  ?>



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
解决kernel_security_check_failure蓝屏的17种方法解决kernel_security_check_failure蓝屏的17种方法Feb 12, 2024 pm 08:51 PM

Kernelsecuritycheckfailure(内核检查失败)就是一个比较常见的停止代码类型,可蓝屏错误出现不管是什么原因都让很多的有用户们十分的苦恼,下面就让本站来为用户们来仔细的介绍一下17种解决方法吧。kernel_security_check_failure蓝屏的17种解决方法方法1:移除全部外部设备当您使用的任何外部设备与您的Windows版本不兼容时,则可能会发生Kernelsecuritycheckfailure蓝屏错误。为此,您需要在尝试重新启动计算机之前拔下全部外部设备。

function是什么意思function是什么意思Aug 04, 2023 am 10:33 AM

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

Win10如何卸载Skype for Business?电脑上的skype怎么彻底卸载方法Win10如何卸载Skype for Business?电脑上的skype怎么彻底卸载方法Feb 13, 2024 pm 12:30 PM

Win10skype可以卸载吗是很多用户们都想知道的一个问题,因为很多的用户们发现自己电脑上的默认程序上有这个应用,担心删除后会影响到系统的运行,下面就让本站来为用户们来仔细的介绍一下Win10如何卸载SkypeforBusiness吧。Win10如何卸载SkypeforBusiness1、在电脑桌面点击Windows图标,再点击设置图标进入。2、点击“应用”。3、在搜索框中输入“Skype”,点击选中找到的结果。4、点击“卸载”。5

JavaScript怎么用for求n的阶乘JavaScript怎么用for求n的阶乘Dec 08, 2021 pm 06:04 PM

用for求n阶乘的方法:1、使用“for (var i=1;i<=n;i++){}”语句控制循环遍历范围为“1~n”;2、循环体中,使用“cj*=i”将1到n的数相乘,乘积赋值给变量cj;3、循环结束后,变量cj的值就n的阶乘,输出即可。

"enumerate()"函数在Python中的用途是什么?"enumerate()"函数在Python中的用途是什么?Sep 01, 2023 am 11:29 AM

在本文中,我们将了解enumerate()函数以及Python中“enumerate()”函数的用途。什么是enumerate()函数?Python的enumerate()函数接受数据集合作为参数并返回一个枚举对象。枚举对象以键值对的形式返回。key是每个item对应的索引,value是items。语法enumerate(iterable,start)参数iterable-传入的数据集合可以作为枚举对象返回,称为iterablestart-顾名思义,枚举对象的起始索引由start定义。如果我们忽

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

MySQL.proc表的作用和功能详解MySQL.proc表的作用和功能详解Mar 16, 2024 am 09:03 AM

MySQL.proc表的作用和功能详解MySQL是一种流行的关系型数据库管理系统,开发者在使用MySQL时常常会涉及到存储过程(StoredProcedure)的创建和管理。而MySQL.proc表则是一个非常重要的系统表,它存储了数据库中所有的存储过程的相关信息,包括存储过程的名称、定义、参数等。在本文中,我们将详细解释MySQL.proc表的作用和功能

foreach和for循环的区别是什么foreach和for循环的区别是什么Jan 05, 2023 pm 04:26 PM

区别:1、for通过索引来循环遍历每一个数据元素,而forEach通过JS底层程序来循环遍历数组的数据元素;2、for可以通过break关键词来终止循环的执行,而forEach不可以;3、for可以通过控制循环变量的数值来控制循环的执行,而forEach不行;4、for在循环外可以调用循环变量,而forEach在循环外不能调用循环变量;5、for的执行效率要高于forEach。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool