function calc($t0, $t1) { $v0 = array_sum(explode(' ', $t0)); $v1 = array_sum(explode(' ', $t1)); $sub = $v1 - $v0; echo "$v1 - $v0 = $sub\n"; return $sub; } $a = array(); $b = array(1); $c = ''; $t0 = microtime(); for ($i = 0; $i < 1E6; $i++) { is_array($a); is_array($b); is_array($c); } $t1 = microtime(); $method1 = calc($t0, $t1); // output: 1420785145.2006 - 1420785142.9861 = 2.2144329547882 $t0 = microtime(); for ($i = 0; $i < 1E6; $i++) { $a === (array) $a; $b === (array) $b; $c === (array) $c; } $t1 = microtime(); $method2 = calc($t0, $t1); // output: 1420785142.986 - 1420785142.2001 = 0.78591012954712 echo ($method1 - $method2) / $method1; // output: 0.64509644428486
從測試中可以知道 is_array() 比較慢。
PHP 5.3.24 (cli) (built: Jun 10 2013 16:42:20) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans