PHP語法性能比較

只是在語法上考慮性能php

實際狀況應該考慮可讀性等問題綜合使用json

1.@性能

@test(); 0.10025715827942 scode

test();  0.09039306640625 sthree

2.deep arraystring

$arr[1][2][3][4][5][6][7] = $i; 0.037128925323486 sio

$arr2[1] = $i;                  0.018270969390869 sfunction

3.defined vartest

$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

相關文章
相關標籤/搜索