http://qs321.pair.com?node_id=1076959


in reply to Using the Substr

If you decide to go with split/join and have quite a few strings to process, consider setting split's LIMIT parameter to 4 (since you're only interested in the first three resulting elements), as this can significantly speed up processing w/o compromising readability:

use strict; use warnings; use Benchmark qw/cmpthese/; my $string = '1:13:1:6:5854:0x00E37F06:0x00D1314C'; sub _split { my $result = join( ':', ( split /:/, $string )[ 0 .. 2 ] ); } sub _split_LIMIT { my $result = join( ':', ( split /:/, $string, 4 )[ 0 .. 2 ] ); } cmpthese( -5, { _split => sub { _split() }, _split_LIMIT => sub { _split_LIMIT() } } );

Output (faster times are lower in the table):

Rate _split _split_LIMIT _split 938083/s -- -25% _split_LIMIT 1248002/s 33% --