Test:

my @a = unpack '(a1)*', $buffer;

Update: But your benchmark is broken. You've included the timing of building the buffer in with the time for split.

Corrected with a couple of alternatives added, substr proves to be fastest.

use strict; use warnings; use Time::HiRes qw[ time ]; my $buffer = "abcdef\x00ghik" x 10_000; my $begin = time(); for( 0 .. 20 ) { my @a = split( //, $buffer ); } printf( "split consumed %.3f second(s)\n", time() - $begin ); $begin = time(); for( 0 .. 20 ) { my @a = unpack( "C*", $buffer ); } printf( "pack consumed %.3f seconds\n", time() - $begin ); $begin = time(); for( 0 .. 20 ) { my @a = unpack( "C*", $buffer ); map{ $_ = chr( $_ ) } @a; } printf( "pack and chr in map consumed %.3f seconds\n", time() - $begin + ); $begin = time(); for( 0 .. 20 ) { my @a = unpack( "(a1)*", $buffer ); } printf( "unpack '{a1)* consumed %.3f seconds\n", time() - $begin ); $begin = time(); for( 0 .. 20 ) { ## Out-by-1 corrected courtesy [AnomalousMonk]. my @a; $#a = length( $buffer )-1; $a[ $_ ] = substr $buffer, $_, 1 for 0 .. $#a; } printf( "substr for consumed %.3f seconds\n", time() - $begin ); exit; __END__ C:\test>junk47 split consumed 2.427 second(s) pack consumed 0.347 seconds ## incomplete pack and chr in map consumed 4.132 seconds unpack '{a1)* consumed 2.527 seconds substr for consumed 1.654 seconds

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.
"I'd rather go naked than blow up my ass"

In reply to Re: Convert string to array - performance challenge by BrowserUk
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.