Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types = 1);
- define('REAL_USAGE', false);
- $items = range(0, 1000000);
- $t = microtime(true);
- $m = memory_get_usage(REAL_USAGE);
- $p = memory_get_peak_usage(REAL_USAGE);
- $results = 0;
- //time: 0.095, memory: 0
- //foreach ($items as $i){
- // if($i % 2){
- // $results++;
- // }
- //}
- //time: 1.4, memory: 22Mib
- //array_walk($items, function($i, $key) use(&$results){
- // if($i % 2){
- // $results++;
- // }
- //});
- $m1 = memory_get_usage(REAL_USAGE);
- $p1 = memory_get_peak_usage(REAL_USAGE);
- $t1 = microtime(true);
- echo $results . PHP_EOL;
- echo sprintf("Execution time: %.6f \n",$t1 - $t);
- echo sprintf("Allocated memory: %d B \n", ($m1 - $m));
- echo sprintf("Peaks difference: %d B \n", ($p1 - $p));
Add Comment
Please, Sign In to add comment