[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: slow str:gmatch(), str:match() pattern
- From: Jonathan Goble <jcgoble3@...>
- Date: Wed, 3 Feb 2016 14:53:13 -0500
On Wed, Feb 3, 2016 at 8:31 AM, Oliver Kroth <email@example.com> 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).