Hacker News new | ask | show | jobs
by letitgo12345 3998 days ago
On OSX 10.1 with luajit 2.1.0 alpha, a straight translation of the C code gets me 59 seconds on my machine which is close to the C speed on it (38 seconds compiled with O3)

Edit: fixed luajit version

1 comments

I've placed my port here: https://gist.github.com/anonymous/ce176d7ab4f6b7b1ba91

If you see anything wrong with it, or odd, feel free to share.

I'm still investigating what is happening to make the run so slow, so if you can find something wrong in my code, that would help.

Lua is global by default. Declare all the variables as local and you'll see significant improvement. Also, there is a boolean type so you can use true and false directly instead of comparing numbers.
I tried using locals, and there was no change to the time. Using a boolean return value for isPrime shaved off two seconds.
if you run it with luajit -jv primes.lua you'll see NYIs about math.mod not implemented.

Replacing math.mod(n, i) with (n % i) gives roughly 9.4x performance.

EDIT: luajit version was LuaJIT 2.0.4 on Mac OSX

Thanks, using % did the trick.