Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: what is the difference between index and rindex

by mreece (Friar)
on Sep 05, 2006 at 20:53 UTC ( #571333=note: print w/replies, xml ) Need Help??

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. )

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://571333]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2022-05-25 14:17 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (90 votes). Check out past polls.