lua-users home
lua-l archive

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


Adam Strzelecki wrote:
> I've made interesting discovery, it seems that using math
> functions from ffi.C, i.e. ffi.C.sin is 2x faster than using
> built-in math.

This is a) not a new discovery, b) not generalizable and c) based
upon an unscientific test case.

First, you're using an expensive FP division and second you're
calling sin() with arguments outside its base range, which is
rather uncommon in practice.

math.sin() calls the x87 fsin instruction, which is known to be
slow on some CPUs, especially when reducing ranges. ffi.C.sin(),
as implemented in most x64 math libraries, uses SSE and a faster
range-reduction algorithm. The call overhead itself is negligible,
since sin() is an expensive operation.

Also, I get very different timings:

    math.sin()  ffi.C.sin()
x86    4.13         4.75
x64    4.13         5.35

I.e. math.sin() is faster on my CPU.

Try the same thing without a division and with sqrt() and you'll
see that math.sqrt() is always faster.


IMHO using sin() in benchmarks is the floating-point equivalent of
Fibonacci benchmarks: totally worthless.

--Mike