Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

It's a tad more efficient to use perl's built-in aliasing mechanism instead of a tie, at the slight cost of using an array reference rather than an array direct.

P:\test>test3 1 2 3 4 It changed 6 So did this 8 9 10 Rate tie sub tie 473/s -- -96% sub 11689/s 2370% -- 0 0 0 -15402 -15402 -15402 -15402 -15402 0 0
#! perl -slw use strict; package Tie::Array::Slice; use Tie::Array; our @ISA = 'Tie::StdArray'; use Data::Dumper; use strict; sub FETCH { ${ $_[0]->[ $_[1] ] } } sub STORE { ref $_[2] ? $_[0]->[ $_[1] ] = $_[2] : ${ $_[0]->[ $_[1] ] } = $_[2]; } package main; use Benchmark qw[ cmpthese ]; sub aliasSlice{ return \@_ } my @a = ( 1 .. 10 ); tie my @b, 'Tie::Array::Slice'; @b = ( \( @a ) )[ 3 .. 7 ]; my $c = aliasSlice( @a[ 3 .. 7 ] ); $b[ 1 ] = 'It changed'; $c->[ 3 ] = 'So did this'; print "@a\n"; our @aliased = (0) x 10; cmpthese( -1, { tie => q[ tie my @alias, 'Tie::Array::Slice'; @alias = ( \( @aliased ) )[ 3 .. 7 ]; $_++ for @alias; ], sub => q[ my $alias = aliasSlice( @aliased[ 3 .. 7 ] ); $_-- for @$alias; ], }); print "\n@aliased"; __END__ P:\test>test3 1 2 3 4 It changed 6 So did this 8 9 10 Rate tie sub tie 473/s -- -96% sub 11689/s 2370% -- 0 0 0 -15402 -15402 -15402 -15402 -15402 0 0

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


In reply to Re: Re: reference to an array slice? by BrowserUk
in thread reference to an array slice? by Anonymous Monk

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (9)
    As of 2015-07-07 07:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (87 votes), past polls