lua-users home
lua-l archive

Re: Lua Performance, IO / Regexp

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


Mariano Kamp wrote:
 Any ideas and hints what I can do to make things faster and/or prettier?
- lpeg: much faster than string match
- no metatable: less code and less function calls (not faster here)
- binary read: no need to parse line endings in io
- the lpeg can probably be made faster (no time to look into it now)
testfile = "o.ap"
hits, hits_index = { }, { }
function whatever(ak) hits[ak] = (hits[ak] or 0) + 1 end
digit = lpeg.R("09")
pattern = (digit * digit * digit * digit * "/" * digit * digit * "/" * digit * digit * "/" * (1-lpeg.S(" ."))^1) / whatever
prefix = "GET /ongoing/When/" * digit * digit * digit * "x/"
suffix = " "
newline = lpeg.S("\n\r")
complete = prefix * pattern * suffix
if true then
 -- binary read 10% faster
 parser = ((1-complete)^0 * complete * (1-newline)^0 * newline)^0
 input_file = io.open(testfile,'rb')
 parser:match(input_file:read("*all"))
 input_file:close()
else
 parser = (1-complete)^0 * complete
 -- 4 times slower
 for line in io.lines(testfile) do
 parser:match(line)
 end
end
for article_key in pairs(hits) do hits_index[#hits_index+1] = article_key end
table.sort(hits_index, function(x,y) return hits[x] > hits[y] end)
for i, article_key in ipairs(hits_index) do
	print(i .. ". " .. article_key .. " : " .. hits[article_key])
	if i == 10 then break end
end
Hans
-----------------------------------------------------------------
 Hans Hagen | PRAGMA ADE
 Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
 tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-----------------------------------------------------------------

AltStyle によって変換されたページ (->オリジナル) /