LanX:
I was thinking along the same lines--I coded up a a little something this morning, but never got around to writing a response. It looks like the code I put together goes well with your comments, so without further ado:
#!/usr/bin/perl
use strict; use warnings; use autodie;
use Benchmark qw[ cmpthese ];
# operate on an array
my $fa_inc = sub { map {$_ + 1} @_ };
my $fa_log = sub { map {log $_} @_ };
my $fa_x3 = sub { map {$_ * 3} @_ };
# edit an array in place
my $fea_inc = sub { map {$_ = $_ + 1} @_ };
my $fea_log = sub { map {$_ = log $_} @_ };
my $fea_x3 = sub { map {$_ = $_ * 3} @_ };
# operate on a scalar
my $fs_inc = sub { 1 + shift };
my $fs_log = sub { log shift };
my $fs_x3 = sub { 3 * shift };
# operate on a scalar by reference
my $fsr_inc = sub { $_[0] + 1 };
my $fsr_log = sub { log $_[0] };
my $fsr_x3 = sub { $_[0] * 3 };
# edit a scalar in place
my $fser_inc = sub { $_[0] = $_[0] + 1 };
my $fser_log = sub { $_[0] = log $_[0] };
my $fser_x3 = sub { $_[0] = $_[0] * 3 };
my (@l1, @l2, @l3);
push @l1, 100*rand for 0 .. 1000;
sub t_fa {
$fa_inc->($fa_log->($fa_x3->(@_)))
}
sub t_fea {
@l3 = @_;
$fea_inc->($fea_log->($fea_x3->(@l3)))
}
sub t_fs {
map { $fs_inc->($fs_log->($fs_x3->($_))) } @_
}
sub t_fsr {
map { $fsr_inc->($fsr_log->($fsr_x3->($_))) } @_
}
sub t_fser {
@l3 = @_;
map { $fser_inc->($fser_log->($fser_x3->($_))) } @l3;
}
# verify that they all have the same result
my (@t, @u);
@t = t_fa(@l1);
@u = t_fea(@l1);
cmp_array('fea', \@t, \@u);
@u = t_fs(@l1);
cmp_array('fea', \@t, \@u);
@u = t_fsr(@l1);
cmp_array('fea', \@t, \@u);
@u = t_fser(@l1);
cmp_array('fea', \@t, \@u);
sub cmp_array {
my ($funcname, $ra1, $ra2) = @_;
my ($la1, $la2) = ($#$ra1, $#$ra2);
die "$funcname: Mismatched length! $la1 != $la2\n"
unless $la1 == $la2;
for (0 .. $la1) {
die "$funcname: Mismatch $_: $ra1->[$_] != $ra2->[$_]\n"
if $ra1->[$_] != $ra2->[$_];
}
}
cmpthese -1, {
fa => q[ @l2 = t_fa(@l1) ],
fea => q[ @l2 = t_fea(@l1) ],
fs => q[ @l2 = t_fs(@l1) ],
fsr => q[ @l2 = t_fsr(@l1) ],
fser => q[ @l2 = t_fser(@l1) ],
};
I didn't explicitly test the case you mentioned (combining the functions into a single one). I was more interested in array operations (fa, fea) vs composing scalar functions and applying them to an array.
In order to make them return the same result without destroying the original array, the "edit in place" functions (fea, fser) perform an array copy. If the original array isn't necessary, you can make it a little faster still--perhaps enough to have the rankings shuffle a little. I was surprised that fs was faster than fser. (I expected that the shift might be a bit more expensive the array indirection.)
$ perl chain_transform.pl
Rate fea fa fser fsr fs
fea 199110/s -- -8% -60% -68% -69%
fa 216392/s 9% -- -57% -66% -67%
fser 499367/s 151% 131% -- -21% -23%
fsr 628278/s 216% 190% 26% -- -4%
fs 651987/s 227% 201% 31% 4% --
...roboticus
When your only tool is a hammer, all problems look like your thumb. |