foreach ($member_sheet -> contents =~ /(\<.*?\/?\>|.*?(?=\<))/g) { ... #### my $buffer = $member_sheet -> contents(); #while ($buffer =~ /\G(<[^<]*>)/scg or $buffer =~ /\G([^<]*(?=<))/scg) { while ($buffer =~ /(<[^>]+>|[^<]+(?=<))/sg) { $_ = $1; ... #### use Benchmark qw(cmpthese); open(my $fh, "<", 'sheet3.xml') or die "Err: $!"; my $str = do { local $/; <$fh>}; cmpthese(-10, { FOR => sub { pos($str) = 0; for ( $str =~ /(\<.*?\/?\>|.*?(?=\<))/g ) { #print "$_\n"; } }, WHILE_1 => sub { pos($str) = 0; while ( $str =~ /\G(<.*?>|.*?(?=<))/scg ) { $_ = $1; #print "$_\n"; } }, WHILE_2 => sub { pos($str) = 0; while ( $str =~ /\G(<.*?>)/scg or $str =~ /\G(.*?(?=<))/scg ) { $_ = $1; #print "$_\n"; } }, WHILE_3 => sub { pos($str) = 0; while ( $str =~ /\G(<[^>]*>)/scg or $str =~ /\G([^<]*(?=<))/scg ) { $_ = $1; #print "$_\n"; } }, }); # Results: s/iter FOR WHILE_3 WHILE_2 WHILE_1 FOR 1.66 -- -27% -31% -35% WHILE_3 1.21 37% -- -6% -11% WHILE_2 1.14 46% 6% -- -5% WHILE_1 1.08 53% 12% 5% --