#!/usr/bin/perl -w use strict; use Benchmark qw(timethese);
my $i = 0; my $str = join '', 'a'..'m'; my @line; push(@line, $str), substr($str, 0, 0, chop $str) for 1..1000;
timethese(-5, { re1 => sub { /^(?=(?:abcdef|ghijklm))/ and ++$i for @line }, re2 => sub { /^(?=abcdef)/ || /^(?=ghijklm)/ and ++$i for @line }, re3 => sub { /^abcdef/ || /^ghijklm/ and ++$i for @line }, idx => sub { index($_, 'abcdef', 0) == 0 || index($_, 'ghijklm', 0) == 0 and ++$i for @line }, });
timethese(-5, { re1 => sub { /^(?=ab..)(?=..ab)/ and ++$i for @line }, re2 => sub { /^(?=ab..)/ && /^(?=..ab)/ and ++$i for @line }, re3 => sub { /^ab../ && /^..ab/ and ++$i for @line }, idx => sub { index($_, 'ab', 0) == 0 && index($_, 'ab', 2) == 2 and ++$i for @line }, });