lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Feb 3, 2016 at 8:31 AM, Oliver Kroth <oliver.kroth@nec-i.de> wrote:
> Hi Erik,
>
> this is mentioned in the Programmin in Lua Book, chapter 21.6 (page 214)
>
>
>
>     line = (' '):rep(50000):match'([^\n]*)\n'
>
>
> takes > 20 seconds on my PC. Adding a start-anchor:

Specifically, when the first scan through the string fails, the match
attempt fails **for the first position in the string**. So it then
tries again from the second position, and then the third, and so
forth. Adding the anchor prevents that.

Since your use case is gmatch, where starting anchors don't work, I
would suggest (completely untested) making the newline optional (i.e.
'([^\n]*)\n?') I would think that would speed things up, though it
will produce an extra match of the empty string at the end of
iteration (and the final "real" match might not have a newline, which
you might need to test for).