in reply to Re^6: How can I do a numeric sort on a substring? (context matters)
in thread How can I do a numeric sort on a substring?
I wrapped all of the routines in @{[...]} to provide the list context; that was what I'd used in the preamble tests.
I added an STss as I had indicated this morning. I decided that STmcs was going to be pretty much the same as STss, so I skipped that one. I did add an mcse which was mcs with map BLOCK replaced by map EXPR.
sub st_sort_substr { @{[ map $_->[0], sort { $a->[1] <=> $b->[1] } map [$_, substr $_, 2], @unordered ]}; } sub map_cat_substr_expr { @{[ map "a-$_", sort { $a <=> $b } map substr($_, 2), @unordered ]}; }
I saw ++swl's post. There wasn't any code there, so I guessed.
use Sort::Key 'ikeysort'; use Sort::Key::Natural 'natsort'; ... sub sort_key_integer { @{[ ikeysort { substr $_, 2 } @unordered ]}; } sub sort_key_natural { @{[ natsort @unordered ]}; }
I ran the benchmark several times; there were no major differences between runs. Here's a sample output, in the spoiler; it's getting very wide (18 subroutines now) and this post is "Re^7", so probably best viewed via the "download" link.
Perl & OS: v5.34.0 on cygwin Unordered data (for preamble tests): a-10 a-01 a-22 a-2 a-0 a-3 a-000 a-1 a-12345 a-1 Preamble tests: grt_pack_expr: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 grt_pack_expr_q: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 sort_key_integer: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 sort_key_natural: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 st_regex: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 st_regex_anchored: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 st_regex_anch_expr_ni: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 st_regex_anch_ni: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 st_regex_expr_ni: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 st_regex_no_index: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 st_sort_substr: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 map_cat_substr: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 map_cat_substr_expr: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 map_cat_substr_len: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 sort_pack: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 sort_regex: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 sort_regex_anchored: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 sort_substr: a-0 a-000 a-01 a-1 a-1 a-2 a-3 a-10 a-22 a- +12345 Legend: GRTpe: grt_pack_expr GRTpeq: grt_pack_expr_q SKi: sort_key_integer SKn: sort_key_natural STr: st_regex STra: st_regex_anchored STraen: st_regex_anch_expr_ni STran: st_regex_anch_ni STren: st_regex_expr_ni STrn: st_regex_no_index STss: st_sort_substr mcs: map_cat_substr mcse: map_cat_substr_expr mcsl: map_cat_substr_len sp: sort_pack sr: sort_regex sra: sort_regex_anchored ss: sort_substr Benchmarks: Note: Unordered data extended with 'map "a-$_", shuffle 0..10000' Rate sra sr sp SKn ss STrn STran STr STra STren ST +raen STss GRTpe GRTpeq mcsl mcs mcse SKi sra 12.0/s -- -1% -59% -61% -76% -81% -81% -81% -81% -81% +-81% -84% -89% -89% -90% -90% -90% -94% sr 12.0/s 1% -- -59% -60% -76% -81% -81% -81% -81% -81% +-81% -84% -88% -89% -90% -90% -90% -94% sp 29.4/s 145% 144% -- -3% -42% -53% -53% -53% -53% -53% +-54% -60% -72% -72% -75% -76% -76% -86% SKn 30.4/s 154% 153% 3% -- -40% -51% -52% -52% -52% -52% +-52% -59% -71% -71% -74% -75% -75% -86% ss 50.4/s 321% 319% 71% 66% -- -19% -20% -20% -20% -20% +-20% -32% -52% -52% -57% -58% -58% -77% STrn 62.4/s 421% 419% 112% 105% 24% -- -0% -0% -0% -0% + -1% -16% -40% -41% -47% -48% -48% -71% STran 62.7/s 424% 421% 113% 106% 24% 0% -- -0% -0% -0% + -1% -15% -40% -40% -47% -48% -48% -71% STr 62.8/s 424% 421% 113% 106% 25% 0% 0% -- 0% -0% + -1% -15% -40% -40% -47% -48% -48% -71% STra 62.8/s 424% 421% 113% 106% 25% 0% 0% 0% -- -0% + -1% -15% -40% -40% -47% -48% -48% -71% STren 62.8/s 424% 421% 113% 106% 25% 0% 0% 0% 0% -- + -1% -15% -40% -40% -47% -48% -48% -71% STraen 63.4/s 429% 426% 116% 108% 26% 1% 1% 1% 1% 1% + -- -15% -39% -40% -46% -47% -47% -71% STss 74.2/s 519% 516% 152% 144% 47% 19% 18% 18% 18% 18% + 17% -- -29% -30% -37% -38% -38% -66% GRTpe 105/s 773% 768% 256% 244% 107% 67% 67% 67% 67% 67% + 65% 41% -- -1% -12% -13% -13% -52% GRTpeq 105/s 779% 774% 258% 246% 109% 69% 68% 68% 68% 68% + 66% 42% 1% -- -11% -13% -13% -51% mcsl 118/s 886% 881% 302% 289% 134% 89% 88% 88% 88% 88% + 86% 59% 13% 12% -- -2% -2% -45% mcs 120/s 905% 900% 310% 296% 139% 93% 92% 92% 92% 92% + 90% 62% 15% 14% 2% -- -0% -44% mcse 120/s 906% 901% 310% 296% 139% 93% 92% 92% 92% 92% + 90% 62% 15% 14% 2% 0% -- -44% SKi 217/s 1708% 1699% 637% 612% 330% 247% 245% 245% 245% 245% +242% 192% 107% 106% 83% 80% 80% --
And here's the code:
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: How can I do a numeric sort on a substring? [Benchmark: reworked and extended]
by swl (Prior) on Jun 28, 2021 at 09:49 UTC |