Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: simple string comparison for efficiency

by etj (Priest)
on Nov 17, 2024 at 17:17 UTC ( [id://11162765]=note: print w/replies, xml ) Need Help??


in reply to simple string comparison for efficiency

I'd use PDL, with some overly-clever finagling to maximise locality:

  • Get each string as columns 0 and 1 of a pdl(byte, 2, 1e8). With PDL's row-major storage, that would put each pair of characters next to each other in memory.
  • Set its "badvalue" to be ord('N'), because the ne operator will ignore that pair if either value is "bad".
  • Run PDL::any(PDL::ne($p->using(0,1)))
  • Sadly this will currently not (yet) loop-fuse the ne and the any, but locality could be maintained by doing a splitdim into smaller chunks, and doing the any on each of those as you go.

Yes, I enjoy that in the third bit, "any" sounds like the normal pronunciation of "ne". I'm only human.

If you want to make a custom transformation that does loop-fusion manually, see the SYNOPSIS of Inline::Pdlpp for an also-bio-inspired example.

Edit: if one chooses to not loop-fuse, the ne produces a mask that is true for any non-matches; that can then be fed into which to give the positions of those.

Edit 2: the above should take a matter of minutes to write, which would be much quicker than writing it in C/C++, and should run pretty quickly. But the naive, 2,1e8 solution has the benefit of broadcasting, which would mean it would also benefit from auto-pthreading, which in C/C++ would be a lot of hard work.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2025-06-17 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.