Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: How Index function works??

by LanX (Saint)
on Oct 24, 2011 at 08:22 UTC ( [id://933309]=note: print w/replies, xml ) Need Help??


in reply to Re: How Index function works??
in thread How Index function works??

when I did speed tests comparing index and a simple m//, the regex (which uses Boyer-Moore) was (mostly) considerably faster.

Since you are linking to v5.14.2 and I'm still using v5.10.0 I suppose that the implementation of index has changed.

Cheers Rolf

Replies are listed 'Best First'.
Re^3: How Index function works??
by Corion (Patriarch) on Oct 24, 2011 at 08:41 UTC

    According to perlreguts, the RE engine also uses fbm_index() to scan for the leftmost atom. There shouldn't be any reason why the performance of the two should differ by a large margin, and I would expect the regular expression to be a bit slower in the general case due to the setup. So I think it's either that your data somehow favours a branch in the RE engine that goes to fbm_index faster, or that the benchmark is not measuring what you want. But I also vaguely remember some thread about such a discrepancy on this site, but I can't find it is index faster than regexp for fixed text token?.

    git blame tells me nobody touched index since 2009, and that change was some refcounting change. The other changes were in 2006.

Re^3: How Index function works??
by davido (Cardinal) on Oct 24, 2011 at 08:26 UTC

    If you don't mind, could you post the benchmark code? I'm just curious to look it over.


    Dave

      Hi davido

      well it was in June, below what I found on my disk:

      There is an "X" in the middle of a 52MB string of repeated alphabet letters ($pattern = join "","a".."z")

      Depending if you look for "Xabc..xyz" or "abc...xyzX" the different approaches show their strength.

      I did more tests which I can't find anymore strongly indicating that index doesn't use Boyer-Moore.

      Just vary the position of the "X". I'd be glad if you looked it over. :)

      use Time::HiRes qw[ time ]; my $pattern = join "","a".."z"; my $str= $pattern x 1E6 . "X" .$pattern x1E6; $\="\n"; $|=1; print "Length: ",length $str; print "\n---End X"; $start=time; print "Match: ", $str =~/${pattern}X/; printf "\t took %.3f sec\n",time-$start; $start=time; print "Index: ", index $str , "${pattern}X"; printf "\t took %.3f sec\n",time-$start; print "\n---Start X"; $start=time; print "Match: ", $str =~/X${pattern}/; printf "\t took %.3f sec\n",time-$start; $start=time; print "Index: ", index $str , "X${pattern}"; printf "\t took %.3f sec\n",time-$start;

      RESULT:

      Length: 52000001 ---End X Match: 1 took 0.021 sec Index: 25999974 took 0.263 sec ---Start X Match: 1 took 0.165 sec Index: 26000000 took 0.094 sec

      Cheers Rolf

        It's not that index doesn't use Boyer Moore, it's more that the regex engine doesn't always use Boyer Moore. If the regex engine comes to the conclusion to use screaminstr, it will use that instead of Boyer Moore.

        I guess that if you change your data to be more favorable to Boyer Moore, the results will reverse as the regular expression will still not use Boyer Moore.

        Update: Even weirder. Looking at the log of use re 'Debug','ALL';, it claims that fbm_instr() is used:

        C:\Projekte>perl -Mre=Debug,ALL -e "shift=~/abcdefghijklmnopqrstuvwxyz +X/" abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef ghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzXabcdefghijklmnopqrstuvw +xyzabcdefghijklmnopqrstuvwxyz Compiling REx "abcdefghijklmnopqrstuvwxyzX" Starting first pass (sizing) >abcdefghij... | 1| reg | | brnc | | piec | | atom Required size 9 nodes Starting second pass (creation) >abcdefghij... | 1| reg | | brnc | | piec | | atom >< | 10| tail~ EXACT <abcdefghijklmnopqrstuvwxyzX> (1 +) -> END first:> 1: EXACT <abcdefghijklmnopqrstuvwxyzX> (9) first at 1 Peep:Pos:0/0 Flags: 0x0 Whilem_c: 0 Lcp: 0 Last:'' 0:0/0 *Fixed:'' @ 0 + Float: '' @ 0/0 Peep> 1: EXACT <abcdefghijklmnopqrstuvwxyzX> (9) join> 1: EXACT <abcdefghijklmnopqrstuvwxyzX> (9) pre-fin:Pos:27/0 Flags: 0x0 Whilem_c: 0 Lcp: 0 Last:'abcdefghijklmnopq +rstuvwxyzX' 27:0/0 *Fixed:'' @ 0 Float: '' @ 0/0 post-fin:Pos:27/0 Flags: 0x0 Whilem_c: 0 Lcp: 0 Last:'abcdefghijklmnop +qrstuvwxyzX' 27:0/0 *Fixed:'' @ 0 Float: '' @ 0/0 commit: Pos:27/0 Flags: 0x0 Whilem_c: 0 Lcp: 0 Last:'abcdefghijklmnopq +rstuvwxyzX' -1:0/0 *Fixed:'abcdefghijklmnopqrstuvwxyzX' @ 0 Fl oat: '' @ 0/0 minlen: 27 r->minlen:0 Final program: 1: EXACT <abcdefghijklmnopqrstuvwxyzX> (9) 9: END (0) anchored "abcdefghijklmnopqrstuvwxyzX" at 0 (checking anchored isall) +minlen 27 r->extflags: CHECK_ALL USE_INTUIT_NOML USE_INTUIT_ML Guessing start of match in sv for REx "abcdefghijklmnopqrstuvwxyzX" ag +ainst "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc defgh"... Check offset min: 0 Start shift: 0 End shift 0 Real End Shift: 0 fbm_instr len=157 str=<abcdefghijklmnopqrst> Found anchored substr "abcdefghijklmnopqrstuvwxyzX" at offset 78... Check offset min:0 max:0 S:78 t:78 D:0 end:157 Starting position does not contradict /^/m... Guessed: match at offset 78 Freeing REx: "abcdefghijklmnopqrstuvwxyzX"

        So, I don't understand what happens, or how the RE engine is faster than index when they both fire up fbm_instr.

Re^3: How Index function works??
by saranrsm (Acolyte) on Oct 24, 2011 at 08:32 UTC
    LanX How big was your file?? I used file sized of 300 MB where for a pattern it took less than second but with regex it took over 5mins and I had to stop the script... I am using perl v5.14
      52 MB and as you can see from my test I'm cosntructing a case where Boyer-Moore must be slower than brute force.

      Cheers Rolf

Log In?
Username:
Password:

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

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

    No recent polls found