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.124
func() 0.424 0.300
undef_func() 0.422 0.298
int_func() 0.343 0.219
$x = self::$x 0.277 0.153
self::$x = 0.351 0.227
isset(self::$x) 0.469 0.345
empty(self::$x) 0.498 0.374
$x = Foo::$x 0.286 0.162
Foo::$x = 0.271 0.147
isset(Foo::$x) 0.319 0.195
empty(Foo::$x) 0.360 0.236
self::f() 1.304 1.180
Foo::f() 1.396 1.272
$x = $this->x 0.392 0.268
$this->x = 0.500 0.376
$this->x += 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.120
isset($this->x) 0.238 0.114
empty($this->x) 0.240 0.116
$this->f() 0.711 0.587
$x = Foo::TEST 0.519 0.395
new 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.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.441 real 0m14.498s
user 0m12.110s
sys 0m0.024s

PHP7.2.1结果:

empty_loop         0.066
func() 0.302 0.236
undef_func() 0.505 0.439
int_func() 0.258 0.192
$x = self::$x 0.269 0.203
self::$x = 0.223 0.157
isset(self::$x) 0.212 0.146
empty(self::$x) 0.213 0.147
$x = Foo::$x 0.220 0.154
Foo::$x = 0.168 0.102
isset(Foo::$x) 0.100 0.034
empty(Foo::$x) 0.116 0.050
self::f() 0.183 0.117
Foo::f() 0.120 0.054
$x = $this->x 0.099 0.033
$this->x = 0.065 0.000
$this->x += 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.094
isset($this->x) 0.173 0.107
empty($this->x) 0.163 0.097
$this->f() 0.135 0.069
$x = Foo::TEST 0.125 0.059
new 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.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.694 real 0m5.756s
user 0m4.521s
sys 0m0.028s

性能提升接近三倍

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

[PHP] PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1)的更多相关文章

  1. Canvas原生API(纯CPU)计算并渲染三维图

    Canvas原生API(纯CPU)计算并渲染三维图 前端工程师学图形学:Games101 第三次作业 利用Canvas画三维中的三角形并使用超采样实现抗锯齿 最终完成功能 Canvas 原生API实现 ...

  2. php5.6.x到php7.0.x特性

    php5.6.x到php7.0.x特性 1.标量类型声明 字符串(string), 整数 (int), 浮点数 (float), 布尔值 (bool),callable,array,self,Clas ...

  3. 如何升级php版本---从php5.5.12 升级php7.1.5 wamp实践

    1.从官网下载一个php7.1.5 2.将刚下载的压缩包解压缩,修改命名为php7.1.5,即php+版本号. 3.将这个文件夹放在wamp/bin/php 目录下. 4.将原来版本的php5.5.1 ...

  4. PHP7 学习笔记(二)PHP5.9 升级到PHP7 遇到的一些坑的记录(php-fpm 图解)

    apache_event_php-fpm 示意图: nginx-php-fpm示意图: Worker-Master-Server TCP-Nginx_PHP Nginx-FastCGI 1.使用$_G ...

  5. [PHP] 2018年终总结

    去掉敏感信息后的不完整版 ==========================================================================2018年12月29日 记 ...

  6. PHP从PHP5.0到PHP7.1的性能全评测

    本文是最初是来自国外的这篇:PHP Performance Evolution 2016, 感谢高可用架构公众号翻译成了中文版, 此处是转载的高可用架构翻译后的文章从PHP 5到PHP 7性能全评测( ...

  7. PHP的性能演进(从PHP5.0到PHP7.1的性能全评测)

    本文是最初是来自国外的这篇:PHP Performance Evolution 2016, 感谢高可用架构公众号翻译成了中文版, 此处是转载的高可用架构翻译后的文章从PHP 5到PHP 7性能全评测( ...

  8. 从PHP5.0到PHP7.1的性能全评测

    本文是最初是来自国外的这篇:PHP Performance Evolution 2016, 感谢高可用架构公众号翻译成了中文版, 此处是转载的高可用架构翻译后的文章从PHP 5到PHP 7性能全评测( ...

  9. 数据库性能高校:CPU使用过高(下)

    CPU使用率过高的常见原因 查询优化器会尽量从CPU,IO和内存资源成本最小的角度,找到最高效的数据访问方式.如果没有正确的索引,或者写的语句本身就会忽略索引, 又或者不准确的统计信息等情况下,查询计 ...

随机推荐

  1. Ubuntu16.04 - 怎么能够更好设置PATH变量,便于管理?

    “/etc/profile”是linux里面的全局变量设置文件,加入这里的PATH变量,全局都可以使用,非常方便.加入时候很简单了,直接在PATH末尾加入":+要加入的变量"就可以 ...

  2. 微信浏览器禁止app下载链接的两种处理方法

    最近替朋友放一个微信下载链接,通过二维码扫描下载. 通过扫描二维码下载APP已成为一个非常方便的方式,微信也成为扫描二维码重要的工具,但是扫描后微信浏览器会对APK和appStore的链接进行屏蔽,导 ...

  3. 微信小程序下可以使用的MD5以及AES加密(通用)

    两段代码都来自网络 ,在小程序下的加解密结果与CS/BS等算出的结果都一致,支持汉字. 一.MD5: var rotateLeft = function (lValue, iShiftBits) { ...

  4. python学习笔记-控制流(if for while break continue)

    if语句 if语句用以检查条件:如果条件为真(True),将运行一块语句(称作 if-block 或 if 块),否则将运行另一块语句(称作 else-block 或 else 块).其中else 从 ...

  5. combining-filters

    The previous two examples showed a single filter in use. In practice, you will probably need to filt ...

  6. php防止网站被刷新

    在实际应用中,总会遇到某些页面被恶意用户刷新.当你的系统在某些模块没有使用缓存的时候,频繁的刷新会导致数据库吃紧.下面附上一段代码,防止频繁的刷新造成的死机情况. 主要是从 session方面进行限制 ...

  7. [RHEL] RHEL7.0 下 Postfix + Dovecot 实现邮件发送

    RHEL7.0 下 Postfix + Dovecot 实现邮件发送 一.前言 大家都对邮件服务(mail service)很感兴趣嘛.我在自己 博客站 预言了自己会实战一次,访问量一天到十几(毕竟平 ...

  8. Python笔记之字典循环

    Python笔记之字典循环   1.问题 Python是一门比较好入门的编程语言,但是入门简单,当然坑也是有的,今天就来介绍一个我遇到的坑吧,也是很简单的一个,就是当时脑子有点转不过弯来了. 先看代码 ...

  9. Laravel5.5 使用第三方Vendor添加注册验证码

    Laravel5系列的验证码添加通用,使用第三方验证码即可完美实现.这里记录下具体步骤吧,以备不时之需. 第一步:使用composer 安装 验证码库 composer require mews/ca ...

  10. JS设计模式之单体模式(Singleton)

    单体模式作为一种软件开发模式在众多面向对象语言中得到了广泛的使用,在javascript中,单体模式也是使用非常广泛的,但是由于javascript语言拥有其独特的面向对象方式,导致其和一些传统面向对 ...