Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Transform Sequence Problem

by roboticus (Canon)
on Jun 19, 2013 at 23:14 UTC ( #1039862=note: print w/ replies, xml ) Need Help??


in reply to Re: Transform Sequence Problem
in thread Transform Sequence Problem

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.


Comment on Re^2: Transform Sequence Problem
Select or Download Code
Re^3: Transform Sequence Problem
by LanX (Canon) on Jun 20, 2013 at 22:34 UTC
    Thanx for testing it out.

    Though I'm not sure what you tried to achieve in 'fea_*()'

    In sub { map {$_ = $_ + 1} @_ }; you are changing the array alias in place but effectively only returning the result from map.

    So whats the intended benefit?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      LanX:

      There's no benefit at all. I was thinking along the lines of testing access speed, and then got to playing around with edit in place without actually realizing that destroying the original and *also* returning a copy of the destroyed array was a worthless enterprise.

      Oh, well, at least I had fun toying around with it.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039862]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-09-23 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (222 votes), past polls