Created
March 26, 2021 07:58
-
-
Save zualex/4e770098f40f2f101138dda7251f32ca to your computer and use it in GitHub Desktop.
foreach without loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function printMemory($start, $header) { | |
printf( | |
"%s - Time: %s | Memory (current): %s KB | Memory (max): %s KB" . PHP_EOL, | |
$header, | |
number_format(round((microtime(true) - $start) * 1000, 6), 6, ',', ''), | |
number_format(round((memory_get_usage() / 1024), 2), 2, ',', ''), | |
number_format(round((memory_get_peak_usage() / 1024), 2), 2, ',', '') | |
); | |
} | |
$big1 = range(0, 100000); | |
$big2 = array_fill(100000, 100000, 'string'); | |
$big3 = range(200000, 100000); | |
$start = microtime(true); | |
$result = []; | |
foreach ($big1 as $key => $value) { | |
$result[$key] = $value; | |
} | |
foreach ($big2 as $key => $value) { | |
$result[$key] = $value; | |
} | |
foreach ($big3 as $key => $value) { | |
$result[$key] = $value; | |
} | |
printMemory($start, 'foreach without loop'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment