前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP语法性能比较

PHP语法性能比较

作者头像
botkenni
发布2022-01-10 09:20:21
2.5K0
发布2022-01-10 09:20:21
举报
文章被收录于专栏:IT码农

只是在语法上考虑性能

实际情况应该考虑可读性等问题综合使用

1.@

@test(); 0.10025715827942 s test(); 0.09039306640625 s

2.deep array

arr[1][2][3][4][5][6][7] = i; 0.037128925323486 s arr2[1] = i; 0.018270969390869 s

3.defined var

a = null; a = 1; 0.011500120162964 s $b = 1; 0.010693073272705 s

4.print vs echo

print($strings); 1.756313085556 s echo $strings; 1.4546310901642 s

5.== v2 ===

if (null == $n) {} 0.015053033828735 s if (null === $n) {} 0.013232946395874 s

6.null vs is_null

if (is_null($n)) {} 0.12696194648743 s if (null === $n) {} 0.021236181259155 s

7.phpversion vs PHP_VERSION

$a = phpversion(); 0.13860487937927 s $a = PHP_VERSION; 0.021455049514771 s

8.sizeof

for (i = 0; i < sizeof(array); i++) {} 0.10716819763184 s count = sizeof(array); for (i = 0; i < count; i++) {} 0.0080151557922363 s

9.quote

$a = "ab{$num}cd";      0.062774181365967 s

$a = 'ab' . $num . 'cd'; 0.055535078048706 s

10.strlen vs isset

if (strlen($strings) > 10) {} 0.091169834136963 s if (isset($strings[10])) {} 0.013430118560791 s

11.three vs ifelse

if (isset($a)) {$b = $a;} else {$b = null;} 0.020678043365479 s

$b = isset($a) ? $a : null;                0.015050888061523 s

12.time() vs $_SERVER['REQUEST_TIME'];

$a = time(); 0.09514594078064 s a = _SERVER['REQUEST_TIME']; 0.020452976226807 s

13.array_walk_recursive

$output = array_fill(1, TIMES / 100, produceString());

function outputFilter(&$value) {

if (is_string($value)) {

$value = preg_replace('/\x{4eba}\x{0f72}\x{0f74}\x{0f84}\x{0f7c}/u', '', $value);

$value = preg_replace('/\x{0963}|\x{0962}|\x{093a}/u', '', $value);

}

}

for ($i = 0; $i < TIMES; $i++) {

array_walk_recursive($output, 'outputFilter');

$json = json_encode($output);

}

565.6918721199 s

$output = array_fill(1, TIMES / 100, produceString());

function outputFilter_u(&$value) {

$value = preg_replace('/\\\u0(f72|f74|f84|f7c|963|962|93a)/u', '', $value);

}

for ($i = 0; $i < TIMES; $i++) {

$json = json_encode($output);

outputFilter_u($json);

}

21.927097082138 s

14.in_array vs isset

if (in_array('1', $array)) {} 732.16136598587 s if (isset($array['1'])) {} 0.018492937088013 s

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/10/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档