use warnings; use strict; use Benchmark qw(cmpthese); use constant N => 100_000 # 10_000 ; my ($ss1, $ss2) = qw(gga gcgccccggc); my $atg = 'atg'; my $stop = 'taa'; my $pad = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; my $repeat = join '', $pad, $atg, $ss1, $stop, $pad, $atg, $ss2, $stop, $pad; my $s = $repeat x N; my @ra; my $cg_la = sub { @ra = $s =~ m{ atg ([acgt]+?) (?= taa|tag|tga) }xmsg }; my $K_la = sub { @ra = $s =~ m{ atg \K [acgt]+? (?= taa|tag|tga) }xmsg }; my $cg_ncg = sub { @ra = $s =~ m{ atg ([acgt]+?) (?: taa|tag|tga) }xmsg }; my $cg_atomic = sub { @ra = $s =~ m{ atg ([acgt]+?) (?> taa|tag|tga) }xmsg }; my $push_cg = sub { @ra = (); push @ra, $1 while $s =~ m{ atg ([acgt]+?) (?= taa|tag|tga) }xmsg }; my $push_KM = sub { @ra = (); push @ra, ${^MATCH} while $s =~ m{ atg \K [acgt]+? (?= taa|tag|tga) }xmsgp }; print "validation... \n"; for my $rx_sub_test ( $cg_la, $K_la, $cg_ncg, $cg_atomic, $push_cg, # $push_KM, ) { $rx_sub_test->(); @ra == 2 * N or die "wrong N matches"; my %h = @ra; 1 == keys %h or die "straight hash: wrong N keys"; $h{$ss1} eq $ss2 or die "straight hash: wrong $ss1 => $ss2"; %h = reverse @ra; 1 == keys %h or die "reverse hash: wrong N keys"; $h{$ss2} eq $ss1 or die "reverse hash: wrong $ss2 => $ss1"; } print "benchmarking... \n"; cmpthese(20, { 'cg_la' => $cg_la, 'K_la' => $K_la, 'cg_ncg' => $cg_ncg, 'cg_atomic' => $cg_atomic, 'push_cg' => $push_cg, # 'push_KM' => $push_KM, });