The second one allocates a whole array and then throws it away. The first one is handled by perl and may be optimized into something much more efficient than it looks. I would not use the second when the first is available. Incidentally you'll find that the following split// variant

my $string = "zero one two three"; $number = (split /\s+/, $string,3)[1];

is more efficient. It needs to be 2 more than the index you want access. One more for the 0/1 base correction, and one more so that the index you want doesnt include additional values afterwards.

A benchmark shows that the variant I posted above is around 300% faster than the array variant you posted.

use Benchmark 'cmpthese'; our $string = "zero one two three four five six seven eight nine te +n"; my $bench={ list => '$number = ( split /\\s+/, $string ) [1]', array => '$number = [ split /\\s+/, $string ]->[1]', list3 => '$number = ( split /\\s+/, $string,3) [1]', }; while (my ($key,$expr)=each %$bench) { printf "%s : %s\n",$key,eval($expr)||die $@; } for my $i (1..3) { cmpthese -3,$bench; sleep(1); } __END__ list : one list3 : one array : one Benchmark: running array, list, list3, each for at least 3 CPU seconds +... array: 3 wallclock secs ( 3.06 usr + 0.00 sys = 3.06 CPU) @ 54 +729.26/s (n=167581) list: 2 wallclock secs ( 3.24 usr + 0.00 sys = 3.24 CPU) @ 85 +398.45/s (n=276264) list3: 4 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 22 +4004.62/s (n=679182) Rate array list list3 array 54729/s -- -36% -76% list 85398/s 56% -- -62% list3 224005/s 309% 162% -- Benchmark: running array, list, list3, each for at least 3 CPU seconds +... array: 3 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 53 +543.26/s (n=166466) list: 3 wallclock secs ( 3.25 usr + 0.00 sys = 3.25 CPU) @ 85 +733.23/s (n=278633) list3: 4 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 22 +6976.21/s (n=705896) Rate array list list3 array 53543/s -- -38% -76% list 85733/s 60% -- -62% list3 226976/s 324% 165% -- Benchmark: running array, list, list3, each for at least 3 CPU seconds +... array: 3 wallclock secs ( 3.02 usr + 0.00 sys = 3.02 CPU) @ 55 +106.76/s (n=166202) list: 3 wallclock secs ( 3.05 usr + 0.00 sys = 3.05 CPU) @ 84 +117.16/s (n=256305) list3: 3 wallclock secs ( 3.14 usr + 0.00 sys = 3.14 CPU) @ 22 +3896.50/s (n=703035) Rate array list list3 array 55107/s -- -34% -75% list 84117/s 53% -- -62% list3 223896/s 306% 166% --

---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: coding style suggestion by demerphq
in thread coding style suggestion: (...)[1] vs. [...]->[1] by thens

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.