daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Sep 7th, 2017 108 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. $filter = function ($item) {
  8.     return $item % 2 === 0;
  9. };
  10. // </preparation>
  11.  
  12. $timingStart = microtime(true);
  13. $memoryStart = memory_get_usage(true);
  14. $peakStart   = memory_get_peak_usage(true);
  15.  
  16. for ($iterations = 0; $iterations < PHP_INT_MAX; $iterations++) {
  17.     // <code>
  18.     $even = array_filter($items, $filter);
  19.     // </code>
  20.     // Timing control:
  21.     if ($iterations % 1000 === 0 &&
  22.         microtime(true) - $timingStart >= EXECUTION_TIME) {
  23.         break;
  24.     }
  25. }
  26.  
  27. $timingDuration = microtime(true) - $timingStart;
  28. $memoryEnd      = memory_get_usage(true);
  29. $memoryUsage    = $memoryEnd - $memoryStart;
  30. $peakEnd        = memory_get_peak_usage(true);
  31. $peakUsage      = $peakEnd - $peakStart;
  32.  
  33. echo "\n\n";
  34.  
  35. printf("Cycles count   : %.2f\n", $iterations);
  36. printf("Cycles by min. : %.2f\n", 60 / $timingDuration * $iterations);
  37. printf("Total time     : %.2f sec.\n", $timingDuration);
  38. printf("Memory usage   : %.2f MB (on end) - %.2f MB (on start) = %.2f MB (delta)\n",
  39.     $memoryEnd / 1048576,
  40.     $memoryStart / 1048576,
  41.     $memoryUsage / 1048576);
  42. printf("Memory peak    : %.2f MB (on end) - %.2f MB (on start) = %.2f MB (delta)\n\n\n",
  43.     $peakEnd / 1048576,
  44.     $peakStart / 1048576,
  45.     $peakUsage / 1048576);
RAW Paste Data
Top