Skip to content

Instantly share code, notes, and snippets.

@joelpittet
Last active February 2, 2016 04:03
Show Gist options
  • Save joelpittet/2978037a059f0bcd7dd5 to your computer and use it in GitHub Desktop.
Save joelpittet/2978037a059f0bcd7dd5 to your computer and use it in GitHub Desktop.

Revisions

  1. joelpittet revised this gist Feb 2, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion perf.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php

    // Run in your Drupal 8 root with `drush scr perf.php`
    use Drupal\Component\Utility\SortArray;

    $data[] = [
  2. joelpittet created this gist Feb 2, 2016.
    111 changes: 111 additions & 0 deletions perf.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    <?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";