#!/usr/bin/perl use warnings; use strict; sub regex_pos { my ($string) = @_; $string =~ /[^ \t]/g; return pos($string) - 1 } sub tr_pos { my ($string) = @_; (my $tr = $string) =~ tr/ \t/!!/c; return index $tr, '!' } for my $s (" \t x ", "\t\t\t\t\t\t \xff...\t ") { regex_pos($s) == tr_pos($s) or die; } use Benchmark qw{ cmpthese }; my $s = ' ' x 200 . "\t" x 200 . "\x01" . " " x 200; cmpthese(-3, { regex => sub { regex_pos($s) }, tr => sub { tr_pos($s) }, }); #### Rate regex tr regex 316530/s -- -67% tr 954355/s 202% -- #### Rate regex tr regex 896519/s -- -6% tr 954359/s 6% --