in reply to Re^3: How to make references more like aliases?
in thread How to make references more like aliases?

Prior Nacre V,
Benchmarking disagrees with you.

Then you misunderstood what I said. I made no claim my code was faster, only that your statement (I generally don't like passing whole arrays), seemed odd given what you were doing is more work than passing a whole array.

Now my goal was being user-friendly, not being fast which is why I preferred $group->[1] over ${ $group->[1] }. But since you did bring up speed, my rudimentary tests show my code as being much faster. Perhaps you should try the following:

$ cat lr.pl #!/usr/bin/perl use strict; use warnings; my @array = 1 .. 250_000; for ( my $i = 250; $i <= @array; $i += 250 ) { my $next = by_groups_of( $i, \@array ); while ( my $group = $next->() ) { } } sub by_groups_of { my ($by, $list) = @_; return sub { () } if ! $by || $by =~ /\D/ || ! @$list; my $pos = 0; my $done; return sub { return () if $done || $pos > $#$list; my $start = $pos; my $stop = $pos + $by - 1 > $#$list ? $#$list : $pos + $by - 1 +; $pos += $by; $done = 1 if $stop == $#$list; return sub { \@_ }->( @$list[ $start .. $stop ] ); } } $ cat pn5.pl #!/usr/bin/perl use strict; use warnings; my @array = 1 .. 250_000; for ( my $i = 250; $i <= @array; $i += 250 ) { my $rc_next = by_groups_of( $i, [ \(@array) ] ); while ( my $ra_group = $rc_next->() ) { last unless @$ra_group; } } sub by_groups_of { my ($by, $ra_list) = @_; $by ||= 0; $ra_list ||= []; my $rc_return = sub { [] }; if ($by && $by !~ /\D/ && @$ra_list) { my $pos = 0; my $done; $rc_return = sub { return [] if $done; my $start = $pos; my $stop = $pos + $by - 1 > $#{$ra_list} ? $#{$ra_list} : +$pos + $by - 1; #$pos += $by - 1; # ?? bug ?? - changed to "$pos += $by; +" $pos += $by; $done = 1 if $stop == $#{$ra_list}; return [ @{$ra_list}[$start .. $stop] ]; }; } return $rc_return; } $ time ./lr.pl real 1m39.558s user 1m36.327s sys 0m0.108s $ time ./pn5.pl real 4m48.570s user 4m25.686s sys 0m0.108s

Cheers - L~R

Replies are listed 'Best First'.
Re^5: How to make references more like aliases?
by Prior Nacre V (Hermit) on Sep 30, 2004 at 03:14 UTC

    This was intended to be friendly, helpful and non-competitive.

    Perhaps you could've replied in a similar vein along these lines:


    Thanks for taking the extra time to compare benchmark results. However, further testing on the intended (platform/OS/Perl version) indicates that my version will run faster and therefore I will be going with that solution.


    Instead of:

    • Using an unsubstantiated claim about what I understand. Are you psychic?
    • Including pointless rhetoric. Neither you nor I claimed your (or my) code was faster. Perhaps take a moment to consider why you added this.
    • Taking my statements out of context.
      • Yet again you have focussed on passing array vs. arrayref while ignoring what is happening in the closure.
      • Claiming I was talking about speed when I was, in fact, talking about speed differences (the former is absolute, the latter is relative).

    Finally, I very much doubt that anyone able to understand the closure generation code will have the slightest problem dereferencing a scalarref. As such, I believe your initial target for improving user-friendliness should be the closure.

    Regards,

    PN5