regex-applicative icon indicating copy to clipboard operation
regex-applicative copied to clipboard

Mysterious run time difference between two different expressions for the same function

Open Glidos opened this issue 2 years ago • 1 comments

I have the contents of a file and wish to apply one regex to the first line and another regex to each of the other lines. This runs fast:

case lines contents of
    [] -> []
    h:b -> (:) <$> match hregex h <*> traverse (match bregex) b

This runs slow:

zipWithM match (hregex : repeat bregex) $ lines contents

That's sort of a shame. Just as I'm writing this, I'm realising that the first is probably faster because reductions can be applied to match bregex just once. Still, I'd be interested to hear your thoughts.

Glidos avatar Jul 30 '23 13:07 Glidos

This is also fast:

zipWithM ($) (match hregex : repeat (match bregex)) $ lines contents,

so it does look like applying match to just the regex is sort of a trick for precompiling the regex

Glidos avatar Jul 30 '23 14:07 Glidos