faster than index?

Hi All,

What is the fastest way to find the index of
a sub string in a string?

-T
0
perl6
2/2/2019 10:57:29 AM
perl.perl6.users 1163 articles. 0 followers. Follow

2 Replies
40 Views

Similar Articles

[PageSpeed] 38

`.index` is going to be the fastest because it is directly using NQP
ops which are built into the VM.

    'abcdefg'.index('de'); # 3

If you want all of the indexes, use `.indices`

    'abcdefabc'.indices('abc'); # 0, 6

`.index` will return `Nil` if it doesn't find anything, and `.indices`
will return an empty list.

    'abc'.index('ZZ'); # Nil
    'abc'.indices('ZZ'); # ()

To see the actual code implementing these (and other similar methods):

- .index: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L255-L284
- .indices: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L206-L252
- .contains: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L183-L203
- .rindex: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L294-L323

On Sat, Feb 2, 2019 at 5:13 AM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> Hi All,
>
> What is the fastest way to find the index of
> a sub string in a string?
>
> -T
0
b2gills
2/2/2019 2:21:56 PM
On 2/2/19 6:21 AM, Brad Gilbert wrote:
> `.index` is going to be the fastest because it is directly using NQP
> ops which are built into the VM.
> 
>      'abcdefg'.index('de'); # 3
> 
> If you want all of the indexes, use `.indices`
> 
>      'abcdefabc'.indices('abc'); # 0, 6
> 
> `.index` will return `Nil` if it doesn't find anything, and `.indices`
> will return an empty list.
> 
>      'abc'.index('ZZ'); # Nil
>      'abc'.indices('ZZ'); # ()
> 
> To see the actual code implementing these (and other similar methods):
> 
> - .index: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L255-L284
> - .indices: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L206-L252
> - .contains: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L183-L203
> - .rindex: https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L294-L323
> 
> On Sat, Feb 2, 2019 at 5:13 AM ToddAndMargo via perl6-users
> <perl6-users@perl.org> wrote:
>>
>> Hi All,
>>
>> What is the fastest way to find the index of
>> a sub string in a string?
>>
>> -T

Thank you!
0
perl6
2/3/2019 2:52:14 AM
Reply: