Guest User

Untitled

a guest
Apr 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. <?php
  2.  
  3. $a = null;
  4. $key = null;
  5. $startIdx = 0;
  6.  
  7. $func = function() use (&$a, &$key, &$startIdx) {
  8. $a = array_fill_keys(range($startIdx, $startIdx+=1000), null);
  9. $key = $startIdx + 1;
  10. };
  11.  
  12. $iterations = 10;
  13. $count = 10000000;
  14. $avg = [];
  15.  
  16. $func();
  17. $name = 'isset';
  18. for ($j = 0; $j < $iterations; ++$j) {
  19. $start = microtime(true);
  20. for ($i = 0; $i<$count; ++$i) {
  21. isset($a[$key]);
  22. }
  23. $time = microtime(true) - $start;
  24. $avg[$name][] = $time;
  25. echo $name . ': ' . $time . "\n";
  26. }
  27.  
  28. $func();
  29. $name = 'array_key_exists';
  30. for ($j = 0; $j < $iterations; ++$j) {
  31. $start = microtime(true);
  32. for ($i = 0; $i<$count; ++$i) {
  33. array_key_exists($key, $a);
  34. }
  35. $time = microtime(true) - $start;
  36. $avg[$name][] = $time;
  37. echo $name . ': ' . $time . "\n";
  38. }
  39.  
  40. $func();
  41. $name = 'isset || array_key_exists';
  42. for ($j = 0; $j < $iterations; ++$j) {
  43. $start = microtime(true);
  44. for ($i = 0; $i<$count; ++$i) {
  45. isset($a[$key]) || array_key_exists($key, $a);
  46. }
  47. $time = microtime(true) - $start;
  48. $avg[$name][] = $time;
  49. echo $name . ': ' . $time . "\n";
  50. }
  51.  
  52. foreach ($avg as $name => $times) {
  53. echo $name . ' [AVG]: ' . \array_sum($times)/\count($times) . "\n";
  54. }
Add Comment
Please, Sign In to add comment