in reply to experiencing slowness due to matching algorithm

I though regex here will be the fastest.

I have no idea why you thought that. Perhaps you could explain your reasoning?

An exact string match and an index match should beat 2 dynamic regexen into a cocked hat for speed.

if ($subpath eq $target || 0 == index $subpath, "$target/") {

Try profiling that and see if it improves matters for you.


Replies are listed 'Best First'.
Re^2: experiencing slowness due to matching algorithm
by ovedpo15 (Pilgrim) on Jan 09, 2023 at 10:36 UTC
    I will try it thanks!
    The problem is that 0 == index $subpath, "$target/" is not quite the same as $subpath =~ /^$target\// because I want it to start with $target. Is there something similar to startswith of Python?

      Please provide a value each for $subpath and $target where 0 == index $subpath, "$target/" is true and where $subpath does not start with $target.

      use strict; use warnings; use Test::More; my @tests = ( { subpath => 'foo/bar', target => 'bar' }, { subpath => 'foo/bar', target => 'ba' }, { subpath => 'foo/bar', target => 'oo' }, ); plan tests => scalar @tests; for my $t (@tests) { isnt index ($t->{subpath}, "$t->{target}/"), 0, "False, as expecte +d" }