lua-users home
lua-l archive

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


% time ../lua-5.1.1/src/lua test/nsieve-bits.lua 11
Primes up to 20480000  1299069
Primes up to 10240000   679461
Primes up to  5120000   356244
106.180u 0.030s 1:46.57 99.6%   0+0k 0+0io 209pf+0w

http://shootout.alioth.debian.org/debian/benchmark.php?test=nsievebits&l
ang=lua

% time ../lua-5.1.1-hex/src/lua test/nsieve-bits-hex.lua 11
Primes up to 20480000  1299069
Primes up to 10240000   679461
Primes up to  5120000   356244
73.310u 0.020s 1:13.56 99.6%    0+0k 0+0io 207pf+0w

% cat nsieve-bits-hex.lua
local BBITS, primes, sz = 64, {}, tohex(10000) << (arg[1] and
tohex(arg[1]) or 1)
if sz < 0x2 then sz = 0x2 end
for m = 0,2 do
  local count, n = 0, tonumber(sz >> m)
  for i = 0,n\BBITS+1 do primes[i] = ~0x0 end
  for i = 2,n do
    local mask = 0x1<<(i%BBITS)
    if primes[i\BBITS] & mask == mask then
      count = count + 1
      for j = i+i, n, i do
        local idx = j\BBITS
        local p, mask = primes[idx], 0x1<<(j%BBITS)
        if p & mask == mask then primes[idx] = p^^mask end
      end
    end
  end 
  print(string.format("Primes up to %8d %8d", n, count))
end

Well part of speed improvement comes from less function calls notably
thanks to the integer division. At least porting from C is easy (just
have to be careful that ^ is not ^^ !). I guess this shall benefit more
from a JIT too.