Hacker News new | ask | show | jobs
by ryanmk 3997 days ago
It would be nice to see the numbers, in addition to the graph.

I did a straight port of C to lua, and ran it with luajit.

The C code ran in 49.901s.

The luajit code ran in 14m16.547s.

C code was compiled with -02 -std=c11 -lm (version 4.8.1, mingw).

luajit.exe was 2.0.3, static build, using VS2012 x64

3 comments

It would also be nice to express numbers in the same base too. :-)

"The luajit code ran in 856.547s."

The graph is log-log which can be a bit tricky to read but that puts it in the same ballpark as Ruby and PHP (assuming the same factor of difference between the two, after scaling your C vs luajit results to match the given C value of ~40s, luajit becomes 687s.)

Did you turn off the JIT? My LuaJIT ran 2.16x slower versus 8x for Lua 5.1, 9.35x 5.2, 10.33 5.3. (I didn't localize the math.sqrt but I know LuaJIT wouldn't care.)

* Oops, I had cheated and used a for loop instead of while. The LuaJIT time was the same but the others are 10.5x, 12.58x, 15.63x.

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

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.