Guest User

Untitled

a guest
Jul 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. define('REAL_USAGE', false);
  5.  
  6. $items = range(0, 1000000);
  7.  
  8. $t = microtime(true);
  9. $m = memory_get_usage(REAL_USAGE);
  10. $p = memory_get_peak_usage(REAL_USAGE);
  11.  
  12. $results = 0;
  13.  
  14. //time: 0.095, memory: 0
  15. //foreach ($items as $i){
  16. // if($i % 2){
  17. // $results++;
  18. // }
  19. //}
  20.  
  21. //time: 1.4, memory: 22Mib
  22. //array_walk($items, function($i, $key) use(&$results){
  23. // if($i % 2){
  24. // $results++;
  25. // }
  26. //});
  27.  
  28. $m1 = memory_get_usage(REAL_USAGE);
  29. $p1 = memory_get_peak_usage(REAL_USAGE);
  30. $t1 = microtime(true);
  31.  
  32. echo $results . PHP_EOL;
  33.  
  34. echo sprintf("Execution time: %.6f \n",$t1 - $t);
  35. echo sprintf("Allocated memory: %d B \n", ($m1 - $m));
  36. echo sprintf("Peaks difference: %d B \n", ($p1 - $p));
Add Comment
Please, Sign In to add comment