| 1 | package Bencher::Scenario::SortingByKey; | ||
| 2 | |||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 4 | 1 | our $DATE = '2019-12-25'; # DATE | |
| 5 | 1 | our $DIST = 'Bencher-Scenario-SortingByKey'; # DIST | |
| 6 | 1 | our $VERSION = '0.002'; # VERSION | |
| 7 | |||
| 8 | 1 | ▶ | use 5.010001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | our $scenario = { | ||
| 13 | summary => 'Benchmark various techniques to sort array by some computed key', | ||
| 14 | participants => [ | ||
| 15 | |||
| 16 | { | ||
| 17 | name => 'uncached', | ||
| 18 | description => <<'_', | ||
| 19 | |||
| 20 | This technique does not cache the sort key and computes it everytime they are | ||
| 21 | compared. This performance of this technique depends on how expensive the | ||
| 22 | computation of key is. (In this benchmark, the computation is very cheap.) | ||
| 23 | |||
| 24 | In Perl code: | ||
| 25 | |||
| 26 | @sorted = sort { GEN_KEY($a) cmp GEN_KEY($b) } @array; | ||
| 27 | |||
| 28 | _ | ||
| 29 | code_template => 'state $array=<array>; sort { -$a <=> -$b } @$array', result_is_list=>1, | ||
| 30 | }, | ||
| 31 | |||
| 32 | { | ||
| 33 | name => 'ST', | ||
| 34 | description => <<'_', | ||
| 35 | |||
| 36 | Schwartzian transform (also known as map/sort/map technique) caches the sort key | ||
| 37 | in an arrayref. It works by constructing, for each array element, a container | ||
| 38 | record (most often anonymous arrayref) containing the original element and the | ||
| 39 | key to be sorted. Later after the sort, it discards the anonymous arrayrefs. The | ||
| 40 | arrayref construction is a significant part of the total cost, especially for | ||
| 41 | larger arrays. | ||
| 42 | |||
| 43 | In Perl code: | ||
| 44 | |||
| 45 | @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, GEN_KEY($_)] } @array; | ||
| 46 | |||
| 47 | _ | ||
| 48 | code_template => 'state $array=<array>; map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -$_] } @$array', result_is_list=>1, | ||
| 49 | }, | ||
| 50 | |||
| 51 | { | ||
| 52 | name => 'GRT', | ||
| 53 | description => <<'_', | ||
| 54 | |||
| 55 | Guttman-Rosler transform, another map/sort/map technique, is similar to ST. The | ||
| 56 | difference is, the computed key is transformed into a fixed-length string that | ||
| 57 | can be compared lexicographically (thus eliminating the need for the Perl custom | ||
| 58 | sort block). The original element is also transformed as a string and | ||
| 59 | concatenated into the string. Thus, GRT avoids the construction of the anonymous | ||
| 60 | arrayrefs. As a downside, the construction of the key string can be tricky. | ||
| 61 | |||
| 62 | In Perl code (assuming the compute key is transformed into a fixed 4-byte | ||
| 63 | string: | ||
| 64 | |||
| 65 | @sorted = map { substr($_, 4) } sort map { pack("NN", -$_, $_) } @array; | ||
| 66 | |||
| 67 | _ | ||
| 68 | code_template => 'state $array=<array>; map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -$_] } @$array', | ||
| 69 | result_is_list=>1, | ||
| 70 | }, | ||
| 71 | |||
| 72 | { | ||
| 73 | name => '2array', | ||
| 74 | description => <<'_', | ||
| 75 | |||
| 76 | This technique caches the compute key in a single array. It also constructs an | ||
| 77 | array of indexes, sorts the array according to the array keys, then constructs | ||
| 78 | the final sorted array using the sorted indexes. | ||
| 79 | |||
| 80 | Compared to GRT, it constructs far fewer anonymous arrayrefs. But it still | ||
| 81 | requires Perl custom sort block. | ||
| 82 | |||
| 83 | In Perl code: | ||
| 84 | |||
| 85 | @indexes = 0 .. $#array; | ||
| 86 | @keys = map { GEN_KEY($_) } @array; | ||
| 87 | @sorted = map { $array[$_] } sort { $keys[$a] <=> $keys[$b] } @indexes; | ||
| 88 | |||
| 89 | _ | ||
| 90 | code_template => 'state $array=<array>; my @keys = map { -$_ } @$array; my @indexes = 0..$#{$array}; map { $array->[$_] } sort { $keys[$a] <=> $keys[$b] } @indexes', | ||
| 91 | result_is_list=>1, | ||
| 92 | }, | ||
| 93 | |||
| 94 | { | ||
| 95 | name => 'Sort::Key::nkeysort', | ||
| 96 | module => 'Sort::Key', | ||
| 97 | function => 'nkeysort', | ||
| 98 | description => <<'_', | ||
| 99 | |||
| 100 | This module also caches the compute keys. It's faster because it's implemented | ||
| 101 | in XS. The compute key must be string (to be compared lexicographically) or | ||
| 102 | numeric. | ||
| 103 | |||
| 104 | _ | ||
| 105 | code_template => 'state $array=<array>; Sort::Key::nkeysort(sub { -$_ }, @$array)', | ||
| 106 | result_is_list => 1, | ||
| 107 | }, | ||
| 108 | |||
| 109 | |||
| 110 | ], | ||
| 111 | datasets => [ | ||
| 112 | 10 | {name=>'10' , args=>{array=>[map {int( 10*rand)} 1..10 ]}}, | |
| 113 | 100 | {name=>'100' , args=>{array=>[map {int( 100*rand)} 1..100 ]}}, | |
| 114 | 1000 | {name=>'1000' , args=>{array=>[map {int( 1000*rand)} 1..1000 ]}}, | |
| 115 | 1 | {name=>'10000', args=>{array=>[map {int(10000*rand)} 1..10000]}}, | |
| 116 | ], | ||
| 117 | }; | ||
| 118 | |||
| 119 | 1 | 1; | |
| 120 | # ABSTRACT: Benchmark various techniques to sort array by some computed key | ||
| 121 | |||
| 122 | __END__ | ||