in reply to opposite of index+rindex? How-to? Needed?
#!/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) }, });
On my machine, it seems about 3 times faster than regex in 5.26.1:
Rate regex tr regex 316530/s -- -67% tr 954355/s 202% --
but only marginally faster in blead perl:
Rate regex tr regex 896519/s -- -6% tr 954359/s 6% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: opposite of index+rindex? How-to? Needed?
by dave_the_m (Monsignor) on Aug 30, 2019 at 07:43 UTC |