lua-users home
lua-l archive

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

Thanks everyone for the tips and tricks.

I changed all the uses of global variables to local variables, got rid of the metatable and substituted table.insert with the table [#table+1] = value - syntax. Combined this resulted in a roughly 10% better performance.

A huge help was to preliminary check the current line with string.find. That reduced the time from about 5 to 3 seconds.

I have not been able to compile lpeg yet, but will have a look at it later.

Quick follow up question to the use of local variables to store functions: When I use local s_find = string.find can I still use the "object oriented" syntax like line ="abc"; hit = line:find("xyz")??


local PATTERN = "GET /ongoing/When/%d%d%dx/(%d%d%d%d/%d%d/%d%d/[^ .]+) "

local hits = {}
local s_match = string.match
local s_find = string.find

for line in io.lines("o100k.ap") do
    if s_find( line, 'GET /ongoing/When/200x/' ) then
	  local article_key = s_match(line, PATTERN)
if (article_key) then hits[article_key] = (hits[article_key] or 0) + 1 end

local hits_index = {}
for article_key in pairs(hits) do
  hits_index[#hits_index+1] = article_key
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