Last active
September 3, 2018 16:42
-
-
Save ivanaugustobd/f9b5705c9ecb26d1a3c3ef1478d82c96 to your computer and use it in GitHub Desktop.
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 | |
$customers = []; | |
for ($count = 1; $count < 100000; $count++) { | |
$customers[] = [ | |
'id' => $count, | |
'name' => array_random(['Lorem', 'Ipsum', 'Dolor']), | |
]; | |
} | |
$runTest = function ($description, $callable) { | |
$startTime = microtime(true); | |
$startMemory = memory_get_usage(); | |
$names = $callable(); | |
$elapsedTime = microtime(true) - $startTime; | |
$totalOfMemory = memory_get_usage() - $startMemory; | |
echo "$totalOfMemory bytes / $elapsedTime seconds using $description" . PHP_EOL; | |
}; | |
$runTest('collect()', function () use ($customers) { | |
return collect($customers)->pluck('name'); | |
}); | |
$runTest('array_map()', function () use ($customers) { | |
return array_map(function ($customer) { | |
return $customer['name']; | |
}, $customers); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment