Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Lua math optimisation? — Gideros Forum

Lua math optimisation?

plamenplamen Member
edited November 2012 in Game & application design
hey guys, any idea about what math optimization are in Lua. How much expensive are trigonometric functions and are there FPU acceleration of ARM compilation for LUA math functions? In other words i know for normal platforms Lua math lib uses co-processor accelerations but what about ARM in mobiles? It will flip all the game math for me if i have to swap math.atan2 and math.sqrt with some silly tables.


  • JaviJavi Member
    Accepted Answer
    Lua math functions are just wrappers that call standard C math functions, like fabs, sinh, etc. So I think it's safe to say that FPU is always used (when is available, of course).

  • I was thinking same but still feel uncertain, would be great to have confirmation from someone who have seen the actual C code+compiling options of math functions :-)
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    As you can see here: http://www.giderosmobile.com/forum/discussion/1266/millisecond-timer#Item_20
    @GregBUG stated that "i used to test to see if lua math.sin / math.cos was slower than (look-up table) sin/cos table."
    and the result:
    "math.sin and math.cos are much more faster!"
  • Additionally, the most important regarding math functions in Lua is to follow some basic rules.
    See pages 17 and 18. http://www.lua.org/gems/sample.pdf
  • keszeghkeszegh Member
    edited November 2012
    i recently changed to lookup tables instead of using the math function all the time, so i was intrigued by the claims in this topic. i decided to make my tests as well.
    i compared four versions with the code below and the results are the following:
    given an integer degree to compute its sine with localizing and lookup table you can do it 2.5 times faster than by simply using the math functions. so at least these tests seem to contradict the statement of "math.sin and math.cos are much more faster!". of course if you have the angle in rad then the gain is less, but it is still significant.
    in any case i never thought about localizing previously, i got this idea from @Javi 's link, and it is a very useful trick as well, maybe more important then using a lookup table.
    for i=0,359 do
    local rad=math.rad
    local sin=math.sin
    local fmod=math.fmod
    local a=0
    for i=1,10000000 do
    a=sinTable[fmod(i,360)]  --v1 --localizing+lookup table:        runs in ~1.6 secs
    --a=sinTable[math.fmod(i,360)] --v2 --lookup table:   	        runs in ~2.0 secs
    --a=sin(rad(i))   --v3  --localizing:  				runs in ~2.7 secs
    --a=math.sin(math.rad(i))   --v4  --no tricks: 			runs in ~3.9 secs
  • Dont know why but on real device (galaxy s3)
    test code run faster using sin/cos func than lookup table.... (on desktop run faster lookup table btw)

    i also I agree with @keszegh that localizing and optimizations (like suggested by @javi) maybe more important that using a lookup table.
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
  • indeed i ran on desktop.
    the results in the same order running on my tegra2 tablet:
    v1 8.1
    v2 9.6
    v3 7.2
    v4 10.1

    so indeed localizing seems to be the most efficient thing. on the tablet the lookup table slows down the process when localizing but it is still a bit faster then doing no tricks at all.
  • The old times where lookup tables had a significant impact on speed are gone, today all devices have FPU so it's not necessary anymore.
Sign In or Register to comment.