Skip to content

Instantly share code, notes, and snippets.

@joelpittet
Last active February 2, 2016 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelpittet/2978037a059f0bcd7dd5 to your computer and use it in GitHub Desktop.
Save joelpittet/2978037a059f0bcd7dd5 to your computer and use it in GitHub Desktop.
uasort vs uasort vs SortArray::suasort performance
<?php
// Run in your Drupal 8 root with `drush scr perf.php`
use Drupal\Component\Utility\SortArray;
$data[] = [
0 => [
'#type' => 'checkbox',
'#title' => 'A',
],
1 => [
'#type' => 'checkbox',
'#title' => 'B',
],
2 => [
'#type' => 'checkbox',
'#title' => 'C',
'#weight' => -1000,
],
];
$data[] = [
0 => [
'#type' => 'checkbox',
'#title' => 'F',
],
1 => [
'#type' => 'checkbox',
'#title' => 'E',
'#weight' => -500,
],
2 => [
'#type' => 'checkbox',
'#title' => 'D',
'#weight' => -1000,
],
];
$data[] = [
0 => [
'#type' => 'checkbox',
'#title' => 'H',
'#weight' => -1000,
],
1 => [
'#type' => 'checkbox',
'#title' => 'I',
'#weight' => 500,
],
2 => [
'#type' => 'checkbox',
'#title' => 'G',
'#weight' => -500
],
];
$data[] = [
0 => [
'#type' => 'checkbox',
'#title' => 'A',
'#weight' => 0,
],
1 => [
'#type' => 'checkbox',
'#title' => 'A',
'#weight' => 0,
],
2 => [
'#type' => 'checkbox',
'#title' => 'A',
'#weight' => 0,
],
];
$alpha = [];
foreach (range('a', 'z') as $letter) {
$alpha[] = [
'#type' => 'checkbox',
'#title' => $letter,
'#weight' => 0,
];
}
$data[] = $alpha;
$numeric = [];
foreach (range(1, 10000) as $num) {
$numeric[] = [
'#type' => 'checkbox',
'#title' => 'A',
'#weight' => $num,
];
}
$data[] = $numeric;
// Run these separately to avoid garbagec collection or other PHP interference.
// $start = microtime(TRUE);
// foreach ($data as $test_data) {
// SortArray::suasort($test_data, ['Drupal\Component\Utility\SortArray', 'sortByWeightProperty']);
// SortArray::suasort($test_data, ['Drupal\Component\Utility\SortArray', 'sortByTitleProperty']);
// }
// print 'SortArray::suasort: ' . round(microtime(TRUE) - $start, 5) . " seconds\n";
// Run these separately to avoid garbagec collection or other PHP interference.
$start = microtime(TRUE);
foreach ($data as $test_data) {
uasort($test_data, ['Drupal\Component\Utility\SortArray', 'sortByWeightProperty']);
uasort($test_data, ['Drupal\Component\Utility\SortArray', 'sortByTitleProperty']);
}
print 'uasort: ' . round(microtime(TRUE) - $start, 5) . " seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment