daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Sep 7th, 2017 110 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. // </preparation>
  8.  
  9. $timingStart = microtime(true);
  10. $memoryStart = memory_get_usage(true);
  11. $peakStart   = memory_get_peak_usage(true);
  12.  
  13. for ($iterations = 0; $iterations < PHP_INT_MAX; $iterations++) {
  14.     // <code>
  15.     $even = [];
  16.     foreach ($items as $item) {
  17.         if ($item % 2 === 0) {
  18.             $even[] = $item;
  19.         }
  20.     }
  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