#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use feature 'say';
# use Benchmark qw(:all) ; # WindowsOS
use Benchmark::Forking qw( timethese cmpthese ); # UnixOS
sub getn_unpack {
return unpack "x" . ($_[1]-1) . "a", $_[0];
}
sub getn_substr {
return substr $_[0], $_[1]-1, 1;
}
sub getn_split {
return +(split //, $_[0])[$_[1]-1];
}
my $strNum = "12345678910";
my $string = "ACATCACCTCCCACAACGAGGACTACACCATCGTGGAACA";
# say getn_unpack($string, 10);
# say getn_substr($string, 10);
# say getn_split($string, 10);
my $results = timethese(1000000000, { 'unpack' => getn_unpack($string, 10),
'substr' => getn_substr($string, 10),
'split' => getn_split($string, 10),
}, 'none');
cmpthese( $results );
__END__
$ perl test.pl
Rate unpack substr split
unpack 171232877/s -- -23% -31%
substr 223713647/s 31% -- -10%
split 248138958/s 45% 11% --
####
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub getn_unpack {
return unpack "x" . ($_[1]-1) . "a", $_[0];
}
my $file = 'data.txt';
my @array;
if (open(my $fh, '<', $file)) {
while (<$fh>) {
chomp;
push @array, getn_unpack($_, 10);
}
} else {
warn "Could not open file '$file' $!\n";
}
print Dumper \@array;
__END__
$ cat data.txt
ACATCACCTCCCACAACGAGGACTACACCATCGTGGAACA
ACATCACCTACCACAACGAGGACTACACCATCGTGGAACA
$ perl test.pl
$VAR1 = [
'C',
'A'
];
##
##
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub getn_split {
return +(split //, $_[0])[$_[1]-1];
}
my $file = 'data.txt';
my @array;
if (open(my $fh, '<', $file)) {
while (<$fh>) {
chomp;
push @array, getn_split($_, 10);
}
} else {
warn "Could not open file '$file' $!\n";
}
print Dumper \@array;
__END__
$ cat data.txt
ACATCACCTCCCACAACGAGGACTACACCATCGTGGAACA
ACATCACCTACCACAACGAGGACTACACCATCGTGGAACA
$ perl test.pl
$VAR1 = [
'C',
'A'
];