Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Does @{ } copy arrays?

by bv (Friar)
on Oct 16, 2009 at 20:06 UTC ( #801637=note: print w/replies, xml ) Need Help??

in reply to Does @{ } copy arrays?

Something I figured out recently is that you can dereference an array ref with $#{} to get the index of the last element. An empty array has it's "last element" at -1, so try this:

if ( $#{ $complete->{landmarks}->{LOG}->[$i][$j][$p] } >= 0 ) { #Whatever }

Update: While that will fix your issue, it doesn't answer the question you asked in the subject. Hopefully someone can answer that authoritatively, but I'll go out on a limb and say that dereferencing shouldn't make a copy of the array. It should tell the interpreter, "There is an array at the end of this pointer." Then the interpreter says, "Thanks, I'll just use that in scalar context now", which should be just fine.

print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

Replies are listed 'Best First'.
Re^2: Does @{ } copy arrays?
by dave_the_m (Monsignor) on Oct 16, 2009 at 21:45 UTC
    Please don't use $#{..} >= 0. As well as being uglier and more long winded than @{...}, it's less efficient in perl 5.10.0 onwards (attaching of PERL_MAGIC_arylen magic to the AV).


      less efficient in perl 5.10.0 onwards (attaching of PERL_MAGIC_arylen magic to the AV).

      Is that going to revert?

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Is that going to revert?
        No. It's a trade off that made $#a slightly slower and bigger while making arrays smaller and faster (as long as you don't access them via the $# mechanism).


      Well, wouldn't $#{}, and @{} in a scalar context need similar magic? Isn't the second just an increment of the first? (There must be more magic anticipated by $#{} to warrant the slowdown?)

      Quantum Mechanics: The dreams stuff is made of

        @a in scalar context just pushes its size onto the stack. $#a in scalar context could be an lvalue or an rvalue, so a 'magic' var is pushed onto the stack that updates the array's size if assigned to. This could probably be optimised when pp_av2arylen is called in rvalue context.


      Interesting! That's the kind of answer I was hoping someone would post. For anyone who's keeping score, here's a benchmark and my results:

      #!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $i=0; cmpthese(-10, { deref => sub { my $aryptr = [qw(Once upon a time in a galaxy far far away +)]; if ( @{ $aryptr } ) { $i=1; } $aryptr = []; if ( @{ $aryptr } ) { $i=-1; } }, arylen => sub { my $aryptr = [qw(Once upon a time in a galaxy far far away +)]; if ( $#{ $aryptr } >= 0 ) { $i=1; } $aryptr = []; if ( $#{ $aryptr } >= 0 ) { $i=-1; } }, } ); __END__ Rate arylen deref arylen 95903/s -- -28% deref 132418/s 38% --

      print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

        The point is that $#a should be faster than @a-1. That's not what you are testing. You have have an extra operation in your $#a test instead of having one less.

        #!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); our @a = qw( Once upon a time in a galaxy far far away ); cmpthese(-3, { cnt_minus_1 => 'my $x = @a-1;', last_idx => 'my $x = $#a;', });
        Rate last_idx cnt_minus_1 last_idx 3225597/s -- -25% cnt_minus_1 4308243/s 34% --
      Uh oh, please don't tell people this, they'll think perl is stupid

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://801637]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2023-12-11 21:03 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.