Re: FFI newby question: importing C functions

  • From: Mike Pall <mike-1206@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 26 Jun 2012 11:27:34 +0200

QuentinC wrote:
> for i=1,n do math.round(i/100) end

This benchmarks the slow division, too. And the result is not
used, so LuaJIT eliminates most of the loop body.

> The pure lua version, even with the addition of the precision
> parameter absent from the C version, looks 2.5 to 3 times faster to
> me (2.4 GHz dual core => 2.15 vs 0.86).

That's only because the MinGW version of round() is incredibly
inefficient. A pure SSE2 version would beat it easily.

> The type check is
> incredibely fast, where standard lua 5.1.4 is very slow (because it
> does an actual string compare).

Type checks in LuaJIT are basically free. Note that the usual
idiom for optional arguments is: p = p or 0

Strings are interned in Lua and LuaJIT -- a string comparison is a
cheap pointer comparison.

What kills the performance for Lua are the hash lookups for
'type', 'math', 'round' or the 10^p and so on. LuaJIT is able to
fold, simplify and/or hoist these checks out of the loop.

--Mike

Other related posts: