• Subject: Re: Fastest way to determine number is integer in plain Lua
• Date: Fri, 7 Nov 2008 21:41:50 +0300

Thanks to everyone who answered my question!

I would settle for brilliant a%1==0.

The trick with 2^52 is great and fast, but IMHO it is a bit too low-level for Lua. If I would need that extra speed it gives, I would seriously consider rewriting whole function to plain C.

BTW, here are the results of my own benchmarking:

lua
-----------------------------------------------------------------------------
name |     rel |   delta | abs / iter = ms per iteration
-----------------------------------------------------------------------------
noop_int |  1.0000 |  0.0000 |   8.83 /   50000000 = 0.176600 ms
noop_nonint |  1.0034 |  0.0300 |   8.86 /   50000000 = 0.177200 ms
bits_int_direct |  1.2650 |  2.3400 |  11.17 /   50000000 = 0.223400 ms
mod_nonint_direct |  1.2684 |  2.3700 |  11.20 /   50000000 = 0.224000 ms
mod_nonint |  1.2707 |  2.3900 |  11.22 /   50000000 = 0.224400 ms
bits_nonint |  1.2741 |  2.4200 |  11.25 /   50000000 = 0.225000 ms
mod_int |  1.2763 |  2.4400 |  11.27 /   50000000 = 0.225400 ms
mod_int_direct |  1.2854 |  2.5200 |  11.35 /   50000000 = 0.227000 ms
bits_int |  1.3307 |  2.9200 |  11.75 /   50000000 = 0.235000 ms
bits_nonint_direct |  1.3465 |  3.0600 |  11.89 /   50000000 = 0.237800 ms
floor_nonint_direct |  1.8732 |  7.7100 |  16.54 /   50000000 = 0.330800 ms
floor_int |  1.8743 |  7.7200 |  16.55 /   50000000 = 0.331000 ms
floor_int_direct |  1.8890 |  7.8500 |  16.68 /   50000000 = 0.333600 ms
floor_nonint |  1.9400 |  8.3000 |  17.13 /   50000000 = 0.342600 ms
luajit -O
-----------------------------------------------------------------------------
name |     rel |   delta | abs / iter = ms per iteration
-----------------------------------------------------------------------------
noop_nonint |  1.0000 |  0.0000 |   1.33 /   50000000 = 0.026600 ms
noop_int |  1.0075 |  0.0100 |   1.34 /   50000000 = 0.026800 ms
bits_int |  1.0677 |  0.0900 |   1.42 /   50000000 = 0.028400 ms
bits_nonint_direct |  1.2105 |  0.2800 |   1.61 /   50000000 = 0.032200 ms
bits_int_direct |  1.2105 |  0.2800 |   1.61 /   50000000 = 0.032200 ms
bits_nonint |  1.2632 |  0.3500 |   1.68 /   50000000 = 0.033600 ms
mod_int |  1.3008 |  0.4000 |   1.73 /   50000000 = 0.034600 ms
mod_int_direct |  1.4211 |  0.5600 |   1.89 /   50000000 = 0.037800 ms
mod_nonint |  1.4361 |  0.5800 |   1.91 /   50000000 = 0.038200 ms
mod_nonint_direct |  1.4737 |  0.6300 |   1.96 /   50000000 = 0.039200 ms
floor_int |  1.5564 |  0.7400 |   2.07 /   50000000 = 0.041400 ms
floor_nonint |  1.6617 |  0.8800 |   2.21 /   50000000 = 0.044200 ms
floor_int_direct |  1.7143 |  0.9500 |   2.28 /   50000000 = 0.045600 ms
floor_nonint_direct |  1.8496 |  1.1300 |   2.46 /   50000000 = 0.049200 ms

It is interesting to see how direct (return OP) vs. indirect (if OP then return true) versions perform. However since numbers are too close I suspect measurement error.

File with benchmarked code is attached as integerbench.lua. Benchmarking tools to run it are attached as kbench.tar.bz2. Run with:

\$ ./kbench.sh 'bench.lua integerbench.lua' 50000000 2>&1 | lua kbenchparse.lua

Alexander.

Attachment: integerbench.lua
Description: Binary data

Attachment: kbench.tar.bz2
Description: BZip2 compressed data