Skip to content

Instantly share code, notes, and snippets.

@ivanaugustobd
Last active September 3, 2018 16:42
Show Gist options
  • Save ivanaugustobd/f9b5705c9ecb26d1a3c3ef1478d82c96 to your computer and use it in GitHub Desktop.
Save ivanaugustobd/f9b5705c9ecb26d1a3c3ef1478d82c96 to your computer and use it in GitHub Desktop.
<?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