zoco

php语法性能比较

2016-04-29


Php近似语法的性能分析

100000次 只是在语法上考虑性能 实际情况应该考虑可读性等问题综合使用 详细代码在 https://github.com/yezuozuo/php-optim

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.== vs ===

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