SHARE
TWEET

Untitled

a guest Sep 7th, 2017 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. const EXECUTION_TIME = 60;
  4.  
  5. // <preparation>
  6. $items = range(0, 10);
  7. function some_filter($item)
  8. {
  9.     return $item % 2 === 0;
  10. }
  11.  
  12. // </preparation>
  13.  
  14. $timingStart = microtime(true);
  15. $memoryStart = memory_get_usage(true);
  16. $peakStart   = memory_get_peak_usage(true);
  17.  
  18. for ($iterations = 0; $iterations < PHP_INT_MAX; $iterations++) {
  19.     // <code>
  20.     $even = array_filter($items, 'some_filter');
  21.     // </code>
  22.     // Timing control:
  23.     if ($iterations % 1000 === 0 &&
  24.         microtime(true) - $timingStart >= EXECUTION_TIME) {
  25.         break;
  26.     }
  27. }
  28.  
  29. $timingDuration = microtime(true) - $timingStart;
  30. $memoryEnd      = memory_get_usage(true);
  31. $memoryUsage    = $memoryEnd - $memoryStart;
  32. $peakEnd        = memory_get_peak_usage(true);
  33. $peakUsage      = $peakEnd - $peakStart;
  34.  
  35. echo "\n\n";
  36.  
  37. printf("Cycles count   : %.2f\n", $iterations);
  38. printf("Cycles by min. : %.2f\n", 60 / $timingDuration * $iterations);
  39. printf("Total time     : %.2f sec.\n", $timingDuration);
  40. printf("Memory usage   : %.2f MB (on end) - %.2f MB (on start) = %.2f MB (delta)\n",
  41.     $memoryEnd / 1048576,
  42.     $memoryStart / 1048576,
  43.     $memoryUsage / 1048576);
  44. printf("Memory peak    : %.2f MB (on end) - %.2f MB (on start) = %.2f MB (delta)\n\n\n",
  45.     $peakEnd / 1048576,
  46.     $peakStart / 1048576,
  47.     $peakUsage / 1048576);
RAW Paste Data
Top