lua-users home
lua-l archive

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


> This once again shows that optimizations should always be based on measurements and not intuition. Intuition lies. :)

As another simple example of this -- that is, that intuition can be misleading
-- consider the two functions below:

function contains1 = function(t, x)
  for _,v in ipairs(t) do
    if v == x then return true end
  end
  return false
end

function contains2 = function(t, x)
  local c = function(_,v) return v == x or nil end
  return table.foreach(t, c) or false
end

Although contains2 uses table.foreach, contains1 is still slightly faster in
my machine (not sure why, though... extra closure?). Well, bottomline is: for
loops are fast, trust them. :)

Cheers,
luis.

-- 
A mathematician is a device for turning coffee into theorems.
        -- P. Erdos 

-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>