Skip to content

Instantly share code, notes, and snippets.

@bwg
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwg/c30413abcd44cee6b60c to your computer and use it in GitHub Desktop.
Save bwg/c30413abcd44cee6b60c to your computer and use it in GitHub Desktop.
is_array($value) vs (array) $value === $value (PHP 5.4.28)
Lee@maverick ~$ php --version
PHP 5.4.28 (cli) (built: May 8 2014 10:11:53)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
Lee@maverick ~$ php arraycasttest.php
--------------------------------------------------
Testing null over 100,000 iterations:
is_array($value) 81.238031387329 ms
(array) $value === $value 20.181179046631 ms
--------------------------------------------------
Testing empty string over 100,000 iterations:
is_array($value) 80.595016479492 ms
(array) $value === $value 26.714086532593 ms
--------------------------------------------------
Testing string value over 100,000 iterations:
is_array($value) 80.502033233643 ms
(array) $value === $value 27.262926101685 ms
--------------------------------------------------
Testing empty array over 100,000 iterations:
is_array($value) 86.398124694824 ms
(array) $value === $value 21.799802780151 ms
--------------------------------------------------
Testing small array(5) over 100,000 iterations:
is_array($value) 84.192991256714 ms
(array) $value === $value 44.742107391357 ms
--------------------------------------------------
Testing med array(20) over 100,000 iterations:
is_array($value) 84.508895874023 ms
(array) $value === $value 106.16588592529 ms
--------------------------------------------------
Testing big array(100) over 100,000 iterations:
is_array($value) 90.351104736328 ms
(array) $value === $value 428.85708808899 ms
--------------------------------------------------
Testing huge array(1000) over 100,000 iterations:
is_array($value) 85.494041442871 ms
(array) $value === $value 4064.0630722046 ms
--------------------------------------------------
average (ms) total (ms)
--------------------------------------------------
is_array 84.160029888153 673.28023910522
cast 592.47326850891 4739.7861480713
<?php
$iterations = 100000;
$values = [
'null' => null,
'empty string' => '',
'string value' => 'string',
'empty array' => [],
'small array(5)' => array_fill(0, 5, 'foo'),
'med array(20)' => array_fill(0, 20, 'foo'),
'big array(100)' => array_fill(0, 100, 'foo'),
'huge array(1000)' => array_fill(0, 1000, 'foo'),
];
$totals = [
'is_array' => 0,
'cast' => 0,
];
foreach ($values as $name => $value) {
echo str_repeat('-', 50)."\n";
echo "Testing {$name} over ".number_format($iterations)." iterations:\n\n";
// -- is_array() ----------------------------------------------------------
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$b = is_array($value);
}
$end = microtime(true);
$duration = ($end - $start) * 1000;
$totals['is_array'] += $duration;
echo str_pad('is_array($value)', 30)."{$duration} ms\n";
// -- (array) $value === $value -------------------------------------------
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$b = (array) $value === $value;
}
$end = microtime(true);
$duration = ($end - $start) * 1000;
$totals['cast'] += $duration;
echo str_pad('(array) $value === $value', 30)."{$duration} ms\n";
}
echo "\n".str_repeat('-', 50)."\n";
echo str_pad('', 10);
echo str_pad('average (ms)', 20);
echo str_pad('total (ms)', 20)."\n";
echo str_repeat('-', 50)."\n";
foreach ($totals as $name => $total) {
echo str_pad($name, 10);
echo str_pad($total/count($values), 20);
echo str_pad($total, 20)."\n";
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment