#!/usr/bin/perl use strict; use warnings; use Time::HiRes; use Benchmark qw/cmpthese/; my $href; sub test1{ $href={}; open(my $fh, "<", "04.txt") or die $!; while(<$fh>){ chomp; push @{ $href->{ substr($_,0,10)} }, [ substr($_,10,10), substr($_,20)]; } close $fh; } sub test2{ my @rec; $href={}; open(my $fh, "<", "04.txt") or die $!; push @{ $href->{ $rec[0] } }, [ @rec[ 1, 2 ] ] while @rec = split '(?<=-[a-z])', <$fh>; close $fh; } sub test3{ #04-1.txt, with delimiter '|' my @rec; $href={}; open(my $fh, "<", "04-1.txt") or die $!; push @{ $href->{ $rec[0]} }, [ @rec[1, 2] ] while @rec = split /\|/, <$fh>; close $fh; } sub test4{ #with unpack my @rec; $href={}; open(my $fh, "<", "04.txt") or die $!; @rec = unpack( 'a10a10a4', $_ ), push @{ $href->{ $rec[0] } }, [ @rec[ 1, 2 ] ] while <$fh>; close $fh; } my %tests = ( '01_substr' => \&test1, '02_split1' => \&test2, '03_split2' => \&test3, '04_unpack' => \&test4, ); cmpthese( -20, #for 20 cpu secs \%tests );