in reply to Re^3: How to make references more like aliases?
in thread How to make references more like aliases?
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 |