[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lpeg pattern repetition
- From: Nick Gammon <nick@...>
- Date: Mon, 2 Jun 2008 15:25:25 +1000
On 02/06/2008, at 2:57 PM, Roberto Ierusalimschy wrote:
Actually, consuming no input is slightly less efficient, because the
machine must consume the input (otherwise how to match it?) and then
backtrack. But in this particular case we have '-line', so it does
not consume input anyway.
I was working out some timings while you wrote that, and my results
agree with your analysis. :)
Further to my question about whether "consuming" the pattern takes
time or not, here are some test results:
local match = "(.*) says, (.*)"
local target = "Nick Gammon says, But does it get goat's blood out?"
local count = 10000000
local who, what
start = os.time ()
for i = 1, count do
who, what = string.match (target, match)
end -- for
period = os.time () - start
print ("Time taken = ", period, " secs") --> 15 seconds
require "lpeg"
local line = lpeg.P " says, "
local patt = lpeg.C((1 - line)^1) * line * lpeg.C (lpeg.P(1)^1)
start = os.time ()
for i = 1, count do
who, what = lpeg.match (patt, target)
end -- for
period = os.time () - start
print ("Time taken = ", period, " secs") --> 12 seconds
local patt = lpeg.C((1 - #line)^1) * line * lpeg.C (lpeg.P(1)^1)
start = os.time ()
for i = 1, count do
who, what = lpeg.match (patt, target)
end -- for
period = os.time () - start
print ("Time taken = ", period, " secs") --> 16 seconds
-------
Summary
-------
All tests produced the same (correct) results.
string.match took 15 seconds
lpeg.match took 12 seconds using (1 - line)
lpeg.match took 16 seconds using (1 - #line)
So it seems that leaving the # out is somewhat faster.
- Nick