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
In reply to Re: Convert string to array - performance challenge
by BrowserUk
in thread Convert string to array - performance challenge
by rtillian
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |