Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: what is the difference between index and rindex

by mreece (Friar)
on Sep 05, 2006 at 20:53 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-29 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found