split has an optimisation that occurs under very specific circumstances.
$ perl -MO=Concise,-exec -e'my @a = split //, $buf;' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s 4 </> pushre(/""/) s/64 5 <#> gvsv[*buf] s 6 <$> const[IV 0] s 7 <@> split[t3] lK 8 <0> pushmark s 9 <0> padav[@a:1,2] lRM*/LVINTRO a <2> aassign[t4] vKS b <@> leave[1 ref] vKP/REFC -e syntax OK $ perl -MO=Concise,-exec -e'our @a = split //, $buf;' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 </> pushre(/""/ => @a) s/64 4 <#> gvsv[*buf] s 5 <$> const[IV 0] s 6 <@> split[t5] vK 7 <@> leave[1 ref] vKP/REFC -e syntax OK

In the second program, the assignment is removed and split stores the result into the array itself. It's as if

our @a = split(//, $buf);
became
split_into(@a, //, $buf);

Unfortunately, it doesn't currently work for lexicals.

$ perl -MO=Concise,-exec -e'my @a; @a = split //, $buf;' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> padav[@a:1,2] vM/LVINTRO 4 <;> nextstate(main 2 -e:1) v:{ 5 <0> pushmark s 6 </> pushre(/""/) s/64 7 <#> gvsv[*buf] s 8 <$> const[IV 0] s 9 <@> split[t3] lK a <0> pushmark s b <0> padav[@a:1,2] lRM* c <2> aassign[t4] vKS d <@> leave[1 ref] vKP/REFC -e syntax OK

When taking advantage of that optimisation, one gets a solution that's almost twice as fast as the previous solution:

Rate regex unpack_C unpack_a split split_pa regex 10.4/s -- -2% -44% -50% -73% unpack_C 10.6/s 2% -- -43% -49% -73% unpack_a 18.8/s 79% 76% -- -10% -52% split 20.9/s 100% 97% 12% -- -46% split_pa 38.8/s 271% 264% 107% 85% --
use strict; use warnings; use Benchmark qw( cmpthese ); my %tests = ( split => q{ my @a = split //, $buf; }, split_pa => q{ local our @a; @a = split //, $buf; }, regex => q{ my @a = $buf =~ /./sg; }, unpack_C => q{ my @a = map chr, unpack 'C*', $buf; }, unpack_a => q{ my @a = unpack '(a)*', $buf; }, ); $_ = "use strict; use warnings; our \$buf; $_ 1" for values(%tests); local our $buf = "abcdef\x00ghik" x 10_000; cmpthese(-2, \%tests);

In reply to Re: Convert string to array - performance challenge (our @a = split) by ikegami
in thread Convert string to array - performance challenge by rtillian

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.