| Posted by
| Nick Gammon
Australia (22,850 posts) bio
Forum Administrator |
| Message
| I added a table capture ( "{| ... |}" ) to your pattern to I could check more accurately what the matches were. For the first match (with the two "and"s) your pattern is still slower. Perhaps if you post exactly how you are timing yours?
====================
Testing: foo and bar and whatever
----------
Nick
1="foo and bar "
2=" whatever"
Time taken = 11.454 us
----------
Albert
1="foo and bar "
2=" whatever"
Time taken = 43.860 us
====================
Testing: foo and bar
----------
Nick
1="foo "
2=" bar"
Time taken = 5.029 us
----------
Albert
1="foo "
2=" bar"
Time taken = 4.749 us
====================
Testing: XandY
----------
Nick
1="X"
2="Y"
Time taken = 4.749 us
----------
Albert
1="X"
2="Y"
Time taken = 5.587 us
====================
Testing: foo
----------
Nick
no match
Time taken = 3.073 us
----------
Albert
no match
Time taken = 3.632 us
====================
Testing: Xand
----------
Nick
1="X"
2=""
Time taken = 4.749 us
----------
Albert
1="X"
2=""
Time taken = 4.470 us
====================
Testing: andY
----------
Nick
1=""
2="Y"
Time taken = 5.029 us
----------
Albert
1=""
2="Y"
Time taken = 6.425 us
====================
Testing: and
----------
Nick
1=""
2=""
Time taken = 4.470 us
----------
Albert
1=""
2=""
Time taken = 4.749 us
====================
Testing:
----------
Nick
no match
Time taken = 3.073 us
----------
Albert
no match
Time taken = 3.352 us
I tried averaging out the results over 20 trials, and removing the table capture. This time I get somewhat faster results:
====================
Testing: foo and bar and whatever
----------
Nick
foo and bar
whatever
Time taken = 1.355 us
----------
Albert
foo and bar
whatever
Time taken = 2.500 us
====================
Testing: foo and bar
----------
Nick
foo
bar
Time taken = 0.726 us
----------
Albert
foo
bar
Time taken = 0.629 us
====================
Testing: XandY
----------
Nick
X
Y
Time taken = 0.615 us
----------
Albert
X
Y
Time taken = 0.545 us
====================
Testing: foo
----------
Nick
no match
Time taken = 0.461 us
----------
Albert
no match
Time taken = 0.405 us
====================
Testing: Xand
----------
Nick
X
Time taken = 0.601 us
----------
Albert
X
Time taken = 0.531 us
====================
Testing: andY
----------
Nick
Y
Time taken = 0.629 us
----------
Albert
Y
Time taken = 0.573 us
====================
Testing: and
----------
Nick
Time taken = 0.824 us
----------
Albert
Time taken = 1.257 us
====================
Testing:
----------
Nick
no match
Time taken = 0.475 us
----------
Albert
no match
Time taken = 0.405 us
(Time is average time).
Code to reproduce:
require "re"
require "tprint"
local ITERATIONS = 20
local result1, result2
print (string.rep ("*", 80))
c = re.compile [[
parse <- {noDelim} lastDelim -- look for all up to the last delimiter followed by the last part
delim <- 'and' -- our delimiter
noDelim <- (!lastDelim .)* -- zero or more characters without the last delimiter
lastDelim <- delim {(!delim .)*} !. -- the delimiter without any more delimiters and then end of subject
]]
pat = re.compile "{g <- &('and' (!'and' .)* !.) / .g} 'and' {.*}" -- Albert Chan pattern
function showResults (result1, result2, start, finish)
if not result1 then
print ("no match")
else
if type (result1) == 'table' then
tprint (result1)
else
print (result1)
print (result2)
end -- if
end -- if
print (string.format ("Time taken = %0.3f us", ((finish - start) / ITERATIONS) * 1e6))
end -- showResults
function test (which)
print (string.rep ("=", 20))
print ("Testing:", which)
print (string.rep ("-", 10))
print "Nick"
start = utils.timer ()
for i = 1, ITERATIONS do
result1, result2 = lpeg.match (c, which)
end -- for
finish = utils.timer ()
showResults (result1, result2, start, finish)
print (string.rep ("-", 10))
print "Albert"
start = utils.timer ()
for i = 1, ITERATIONS do
result1, result2 = lpeg.match (pat, which)
end -- for
finish = utils.timer ()
showResults (result1, result2, start, finish)
end -- test
tests = {
"foo and bar and whatever",
"foo and bar",
"XandY",
"foo",
"Xand",
"andY",
"and",
"",
}
for _, v in ipairs (tests) do
test (v)
end -- for
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|