lua-users home
lua-l archive

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




But I would be interested in the performance implications.

steve d.

With the trivial change of adding the line  "set_mt.__unm = set_mt.__len"
so that Lua 5.1 can also do it, the attached benchmark gives on my machine:

               non-set   set
lua5.1       0.704   1.344
lua5.2       0.616   0.952
luajit        0.168   0.180






set = require "set"

function count(s)
   local n=0
   for k in pairs(s) do n=n+1 end
   return n
end

function ranset(N,m)
   local s
   if arg[1]=='s' then s=set{} else s={}; setmetatable(s,{__unm=count}) end
   for k=1,N do s[math.random(m)]=1 end
   return s
end

maxit=1000
size=1000
limit=2000
ave=0

for iter=1,maxit do s=ranset(size,limit); ave=ave+(-s) 
end

print ("average set size = ",ave/maxit)