optimization - Table access vs function call + conditional determination: which is faster? -
i need check if particular string 1 of set of predetermined strings.
two methods came mind: setting table return true on particular value
local isparticular = { [string1] = true, [string2] = true } print(isparticular[string1]) -- true print(isparticular[randomstring]) -- nil -> false
or setting function check conditional determination
function isparticular(s) return s == string1 or s == string2 end print(isparticular(string1)) -- true print(isparticular(randomstring)) -- false
from understand table method take same time both of particular strings , different strings, while function call because of short-circuit evaluation take less time string1
, more time string2
, randomstring
.
also, both function call , table access known causing little overhead, maybe short-circuit evaluation can make difference (in being slower think, considered have more 2 particular strings , of times string won't match of them).
so method should use?
a hash-table lookup outperform functional lookup large dataset. so, go first method:
local isparticular = { string1 = true, string2 = true } print(isparticular[string1]) -- true print(isparticular[randomstring]) -- nil -> false
Comments
Post a Comment