Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Or should I simply go with "No, I won't pay any attention to the man behind the curtain."?

Yes. Perl functions generally do what they say on the tin :-)

Admitted, this is not one of the all-time consuming questions about the Universe, but when something doesn't act the way I expect, I like to know how it achieves the result(s) that it gets.

As far as I can tell from the internal implementation of pp_unshift and av_unshift, your guess that it's the latter (Edit 2: that is, shift the array up by N at once) appears to be correct. But as stated above, that should be considered an implementation detail that you don't need to worry about.

My personal bet is on the second as it'd be faster.

If concerned about performance, always measure first, using Benchmark and/or Devel::NYTProf.

The following is kind of a pointless comparison, instead it's just intended to show how one might use the former of the two modules.

use warnings; use strict; use Benchmark qw/cmpthese/; cmpthese(-2, { plain => sub { my @array = 1..50; my @add = 100..150; unshift @array, @add; }, reverse => sub { my @array = 1..50; my @add = 100..150; unshift @array, reverse @add; }, loop => sub { my @array = 1..50; my @add = 100..150; unshift @array, $_ for @add; }, }); __END__ Rate loop reverse plain loop 278364/s -- -31% -32% reverse 400772/s 44% -- -2% plain 407520/s 46% 2% --

Update: Actually, LanX's post here provides something more interesting to benchmark. I've also added a built-in test of the benchmarked code here.

use warnings; use strict; use Benchmark qw/cmpthese/; use constant TEST => 0; my $EXP = join $", 100..150, 1..50; cmpthese(-2, { unshift => sub { my @array = 1..50; my @add = 100..150; unshift @array, @add; "@array" eq $EXP or die "@array" if TEST; }, loop => sub { my @array = 1..50; my @add = 100..150; unshift @array, $_ for reverse @add; "@array" eq $EXP or die "@array" if TEST; }, concat => sub { my @array = 1..50; my @add = 100..150; @array = ( @add, @array ); "@array" eq $EXP or die "@array" if TEST; }, }); __END__ Rate loop concat unshift loop 273062/s -- -19% -33% concat 337646/s 24% -- -18% unshift 409595/s 50% 21% --

In reply to Re^3: I am confused by a "Learning Perl" sample showing "unshift" (updated) by haukex
in thread I am confused by a "Learning Perl" sample showing "unshift" by KenAndrews

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 romping around the Monastery: (3)
As of 2024-04-26 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found