Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found