0

0

提升PHP性能之改变Zend引擎分发方式

php中文网

php中文网

发布时间:2016-06-21 08:52:46

|

1334人浏览过

|

来源于php中文网

原创

 

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

  ?>



数码产品性能查询
数码产品性能查询

该软件包括了市面上所有手机CPU,手机跑分情况,电脑CPU,电脑产品信息等等,方便需要大家查阅数码产品最新情况,了解产品特性,能够进行对比选择最具性价比的商品。

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

928

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

307

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

183

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

29

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

103

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

54

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

17

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

764

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

92

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 18.4万人学习

Pandas 教程
Pandas 教程

共15课时 | 1.1万人学习

C# 教程
C# 教程

共94课时 | 9.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号