Last active
March 13, 2020 10:38
-
-
Save KVytyagov/b9ce9d3f373e87053e729f9e7a561018 to your computer and use it in GitHub Desktop.
Test performance of isset, array_key_exists, isset OR array_key_exists on NULL-array item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = null; | |
$key = null; | |
$startIdx = 0; | |
$func = function() use (&$a, &$key, &$startIdx) { | |
$a = array_fill_keys(range($startIdx, $startIdx+=1000), null); | |
$key = $startIdx + 1; | |
}; | |
$iterations = 10; | |
$count = 1000000; | |
$avg = []; | |
$func(); | |
$name = 'isset'; | |
for ($j = 0; $j < $iterations; ++$j) { | |
$start = microtime(true); | |
for ($i = 0; $i<$count; ++$i) { | |
isset($a[$key]); | |
} | |
$time = microtime(true) - $start; | |
$avg[$name][] = $time; | |
echo $name . ': ' . $time . "\n"; | |
} | |
$func(); | |
$name = 'array_key_exists'; | |
for ($j = 0; $j < $iterations; ++$j) { | |
$start = microtime(true); | |
for ($i = 0; $i<$count; ++$i) { | |
array_key_exists($key, $a); | |
} | |
$time = microtime(true) - $start; | |
$avg[$name][] = $time; | |
echo $name . ': ' . $time . "\n"; | |
} | |
$func(); | |
$name = 'isset || array_key_exists'; | |
for ($j = 0; $j < $iterations; ++$j) { | |
$start = microtime(true); | |
for ($i = 0; $i<$count; ++$i) { | |
isset($a[$key]) || array_key_exists($key, $a); | |
} | |
$time = microtime(true) - $start; | |
$avg[$name][] = $time; | |
echo $name . ': ' . $time . "\n"; | |
} | |
$func(); | |
$a = array_keys($a); | |
$name = 'in_array'; | |
for ($j = 0; $j < $iterations; ++$j) { | |
$start = microtime(true); | |
for ($i = 0; $i<$count; ++$i) { | |
in_array($key, $a); | |
} | |
$time = microtime(true) - $start; | |
$avg[$name][] = $time; | |
echo $name . ': ' . $time . "\n"; | |
} | |
$func(); | |
$a = array_keys($a); | |
$name = 'in_array_strict'; | |
for ($j = 0; $j < $iterations; ++$j) { | |
$start = microtime(true); | |
for ($i = 0; $i<$count; ++$i) { | |
in_array($key, $a, true); | |
} | |
$time = microtime(true) - $start; | |
$avg[$name][] = $time; | |
echo $name . ': ' . $time . "\n"; | |
} | |
foreach ($avg as $name => $times) { | |
echo $name . ' [AVG]: ' . \array_sum($times)/\count($times) . "\n"; | |
} |
PHP 7.2.28-3+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Feb 23 2020 07:23:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.28-3+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.9.2, Copyright (c) 2002-2020, by Derick Rethans
isset [AVG]: 0.20750651359558
array_key_exists [AVG]: 0.88147220611572
isset || array_key_exists [AVG]: 0.97458655834198
in_array [AVG]: 5.1713105201721
in_array_strict [AVG]: 9.7148007154465
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP 7.0.33-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Dec 7 2018 08:28:27) ( NTS )
isset: 0.84976196289062
isset: 0.82834601402283
isset: 0.83587598800659
isset: 0.82731986045837
isset: 0.99396395683289
isset: 1.2600409984589
isset: 1.3517770767212
isset: 1.3481690883636
isset: 1.3409090042114
isset: 1.4142439365387
array_key_exists: 4.1361770629883
array_key_exists: 4.0822560787201
array_key_exists: 4.7288780212402
array_key_exists: 5.2399561405182
array_key_exists: 4.3911950588226
array_key_exists: 4.6153249740601
array_key_exists: 5.1126308441162
array_key_exists: 4.776046037674
array_key_exists: 4.9200160503387
array_key_exists: 4.7212162017822
isset || array_key_exists: 6.5889999866486
isset || array_key_exists: 5.7212750911713
isset || array_key_exists: 5.3783040046692
isset || array_key_exists: 5.3733489513397
isset || array_key_exists: 7.1300749778748
isset || array_key_exists: 6.4987850189209
isset || array_key_exists: 7.5291221141815
isset || array_key_exists: 7.9868159294128
isset || array_key_exists: 7.0143849849701
isset || array_key_exists: 7.2444880008698
isset [AVG]: 1.1050407886505
array_key_exists [AVG]: 4.6723696470261
isset || array_key_exists [AVG]: 6.6465599060059