lua-users home
lua-l archive

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



My next step is to implement slide_over_read in C, but prior to that I thought you guys
may have some ideas on how to improve it.

That chunk will be called million of times, that's why I want to squeeze as many cpu
cycles as possible.

-drd

Are you using LuaJit or have you tried using it? Have you profiled the code?


On 19 April 2011 16:58, David Rio Deiros <driodeiros@gmail.com> wrote:
I was wondering if I could get some feedback on the following chunk of
lua code. Since I did not get any replies in my previous email I thought
I'd try to be more specific:

-------------------------------------------------------------------------
local function slide_over_read(read, pl)
 local i      = 1
 local ps     = 7
 local fs     = 3
 local n_hits = 0

 while ps + i <= #read+1 do -- while the window is within the size of the read
   sub_read = read:sub(i, ps+i-1)

I do not understand why you do not just set ps to 8 and then increment ps later, instead of the addition in the while and the in the string.sub . I also do not understand why you add one to the size of read and then check for less or equal, why not just drop the addition and check for less than?
 
   nt_value = sub_read:sub(fs+1, fs+1)
   sub_read = sub_read:sub(1, fs) .. "N" .. sub_read:sub(fs+2)
 
Shouldn't both nt_value and sub_read be locals? Is string.format quicker? Does concatenation create more temporary strings?
 
   print("Trying: " .. sub_read)

This line I would almost certainly remove if it is being called as much as you say, io is slow. 

   if pl[sub_read] then -- We have a probe with that sequence
     if not pl[sub_read].hits then -- No previous hits
       pl[sub_read].hits = {A=0, C=0, G=0, T=0, N=0}
     end
     pl[sub_read].hits[nt_value] = pl[sub_read].hits[nt_value] + 1
     n_hits = n_hits + 1
   end
Maybe cache pl[sub_read] as a local? 

   i = i + 1
 end
 return n_hits
end

-- Main
probes = {}
probes["123N567"] = {}
read = "123A56789"
print("hits: " .. slide_over_read(read, probes))
print("--> " .. probes["123N567"].hits["A"])
print("---------------")

probes["345N789"] = {}
read = "12345T789"
print("hits: " .. slide_over_read(read, probes))
print("--> " .. probes["345N789"].hits["T"])
-------------------------------------------------------------------------