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


In reply to Re^4: How to make references more like aliases? by Limbic~Region
in thread How to make references more like aliases? by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.