Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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 = 10000000;
- $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";
- }
- foreach ($avg as $name => $times) {
- echo $name . ' [AVG]: ' . \array_sum($times)/\count($times) . "\n";
- }
Add Comment
Please, Sign In to add comment