G'day Dean,
"List::Util::pairmap would be an existing solution very similar to the new feature. It also seems to run quite quickly (but I can't compare it to for_list since I don't have a new-enough perl)."
No problem. I have v5.36.0 installed. I added your code to the OP benchmark code.
Mine starts as follows.
Refer to "perl5360delta: use v5.36"
for why I removed the strict and warnings pragmata.
#!/usr/bin/env perl
use v5.36;
use List::Util qw /pairs pairvalues pairmap/;
I then added:
-
say a_pairmap(); after the existing eight say statements.
-
a_pairmap => \&a_pairmap, to the end of the cmpthese hashref.
-
Your sub a_pairmap { ... } verbatim, at the end of the script.
I ran the benchmark five times.
All results were pretty much the same; here's a median set:
2500050000
2500050000
2500050000
2500050000
2500050000
2500050000
2500050000
2500050000
2500050000
Rate a_pairs_alias a_pairs h_each h_by_key h_for_list
+h_vals a_pair_vals a_pairmap a_for_list
a_pairs_alias 48.8/s -- -4% -67% -73% -83%
+ -90% -90% -92% -94%
a_pairs 51.0/s 4% -- -66% -71% -82%
+ -90% -90% -91% -94%
h_each 150/s 207% 193% -- -16% -47%
+ -69% -71% -74% -82%
h_by_key 178/s 265% 249% 19% -- -37%
+ -64% -65% -69% -78%
h_for_list 284/s 482% 457% 90% 59% --
+ -42% -45% -51% -66%
h_vals 490/s 903% 860% 227% 175% 72%
+ -- -5% -16% -41%
a_pair_vals 513/s 951% 906% 243% 188% 81%
+ 5% -- -12% -38%
a_pairmap 582/s 1091% 1041% 289% 226% 105%
+ 19% 13% -- -30%
a_for_list 827/s 1594% 1521% 453% 364% 191%
+ 69% 61% 42% --
In all runs, a_pairmap was second fastest; and,
h_by_key was faster than h_each (which differs from the OP results).
My environment:
$ perl -v | head -2 | tail -1
This is perl 5, version 36, subversion 0 (v5.36.0) built for cygwin-th
+read-multi
$ uname -a
CYGWIN_NT-10.0 titan 3.2.0(0.340/5/3) 2021-03-29 08:42 x86_64 Cygwin
|