http://qs1969.pair.com?node_id=571333


in reply to what is the difference between index and rindex

fwiw, rindex appears to be slightly faster:
use Benchmark qw(timethese); my $s = "abcdefg"; timethese( 6_000_000, { rindex => sub { my $i = rindex( $s, "cde" ) }, index => sub { my $i = index( $s, "cde" ) }, } );
Benchmark: timing 6000000 iterations of index, rindex...
index: 1 wallclock secs ( 1.14 usr + 0.00 sys = 1.14 CPU) @ 5263157.89/s (n=6000000)
rindex: 1 wallclock secs ( 1.02 usr + 0.00 sys = 1.02 CPU) @ 5882352.94/s (n=6000000)
their functional difference is that rindex starts from the end of the string, and index starts at the beginning. there is no difference in their usage.

Replies are listed 'Best First'.
Re^2: what is the difference between index and rindex
by ikegami (Patriarch) on Sep 06, 2006 at 16:05 UTC

    For me, index is faster. In any case the difference is less than 10% (for both me and you), so the test is not conclusive. ( Even if it were conclusive, the difference is rather minor. Of course, this is all moot since the two functions are not interchangeable. )