Created
August 25, 2014 06:12
-
-
Save PowerKiKi/9571aea8fa8d6160955f to your computer and use it in GitHub Desktop.
PHP array_map memory usage
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 | |
// Memory benchmark related to http://stackoverflow.com/questions/11657835/how-to-get-a-one-dimensional-scalar-array-as-a-doctrine-dql-query-result | |
// Build an array of 50 MiB | |
$data = []; | |
for ($i = 0; $i < 1024; $i++) { | |
$data[] = ['id' => str_repeat(' ', 1024 * 50)]; // store 50kb more | |
} | |
// Variant 1, will double memory usage to 100 MiB, if uncommented | |
$ids1 = []; | |
//$ids1 = array_map('current', $data); | |
// Variant 2, will stay at 50 MiB | |
$ids2 = []; | |
foreach ($data as $d) { | |
$ids2[] = $d['id']; | |
} | |
echo 'peak memory: ' . (memory_get_peak_usage() / 1024 / 1024) . " MiB\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment