note 108875 deleted from function.is-array by danbrown
| From: | danbrown@php.net | Date: | Thu, 31 May 2012 16:48:22 +0000 |
| Subject: | note 108875 deleted from function.is-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188343@lists.php.net to get a copy of this message | ||
Note Submitter: skypppher at gmail dot com
----
Another speed test. Since hperrin found his "speedy" test and JTS's results differ
from hperrin's I've ran my own test, a bit more difficult.
Testing conditions:
1. Each condition situation sould be measured separate from others.
2. Time, consumed by computings (not evaluating conditions) sould be substracted from results.
3. While there could appear performance lags on server each test is repeated for 10 times and small
delay is inserted between tests.
4. Variable is supposed to exist, because suppressing E_WARNING if it doesn't ('@' in
'if' statement) takes a lot of time.
Here is the code:
<?php
echo "My PHP version: " . PHP_VERSION . PHP_EOL;
$test_loops = 100000;
$round_loops = 10;
$arr = array(array('1', '2'), 'string', array());
$arr2 = array(true, false, true);
$delay1 = $delay2 = $delay3 = $delay4 = array(0, 0, 0);
$arrays1 = $arrays2 = $arrays3 = $arrays4 = 0;
for ($j = 0; $j < $round_loops; $j++) {
foreach ($arr as $k => $v) {
$dummy_test = $arr2[$k];
$delay1[$k] -= microtime(true);
for ($i = 0; $i < $test_loops; $i++) if (((array)$v === $v)) { $arrays1++; } //succeeds
$delay1[$k] += microtime(true);
$delay2[$k] -= microtime(true);
for ($i = 0; $i < $test_loops; $i++) if ((is_array($v))) { $arrays2++; } //succeeds
$delay2[$k] += microtime(true);
$delay3[$k] -= microtime(true);
for ($i = 0; $i < $test_loops; $i++) if ((gettype($v) == 'array')) {
$arrays3++; } //succeeds
$delay3[$k] += microtime(true);
$delay4[$k] -= microtime(true);
for ($i = 0; $i < $test_loops; $i++) if ($dummy_test) { $arrays4++; } //dummy computing
$delay4[$k] += microtime(true);
}
usleep(100000);
}
for ($i = 0; $i < 3; $i++) {
$delay1[$i] -= $delay4[$i];
$delay2[$i] -= $delay4[$i];
$delay3[$i] -= $delay4[$i];
}
echo "Testing $round_loops times, each cicle consists of $test_loops loops." . PHP_EOL;
echo "Test1. Testing with '(array)...===...'. Found $arrays1 arrays." . PHP_EOL;
echo "Not empty array: " . $delay1[0] . "s. Not an array: " . $delay1[1] .
"s. Empty array: " . $delay1[2] . "s" . PHP_EOL;
echo "Test2. Testing with 'is_array()'. Found $arrays2 arrays." . PHP_EOL;
echo "Not empty array: " . $delay2[0] . "s. Not an array: " . $delay2[1] .
"s. Empty array: " . $delay2[2] . "s" . PHP_EOL;
echo "Test3. Testing with 'gettype(...) == 'array''. Found $arrays3
arrays." . PHP_EOL;
echo "Not empty array: " . $delay3[0] . "s. Not an array: " . $delay3[1] .
"s. Empty array: " . $delay3[2] . "s" . PHP_EOL;
?>
My output is as follows:
My PHP version: 5.3.8
Testing 10 times, each cicle consists of 100000 loops.
Test1. Testing with '(array)...===...'. Found 2000000 arrays.
Not empty array: 2.1087992191315s. Not an array: 2.1430599689484s. Empty array: 0.88696908950806s
Test2. Testing with 'is_array()'. Found 2000000 arrays.
Not empty array: 0.81196475028992s. Not an array: 0.78999948501587s. Empty array: 0.76609230041504s
Test3. Testing with 'gettype(...) == 'array''. Found 2000000 arrays.
Not empty array: 1.3605468273163s. Not an array: 1.3352777957916s. Empty array: 1.3249514102936s
On "not empty arrays" or "not an array" fastest is 'is_array()' with
big advantage. On "empty arrays" fastest is also 'is_array()' but with almost
same result as 'CAST' method.
I guess results differ from version to version 'cause I get something very different from
previous tests. Try for yourself!