I've set out on a quest for what some might argue is needless optimisation. One suggestion that I received on another forum post was to calculate some things inline, so I tried out substituting math.sqrt with ^0.5 and I couldn't explain my results.

local mathSqrt = math.sqrt local getTimer = system.getTimer local t1, t2 = {}, {} local iterations = 100000 local number = 123456789 local start1 = getTimer() for i = 1, iterations do t1[i] = mathSqrt(123456789) -- t1[i] = mathSqrt(number) end local end1 = getTimer()-start1 local start2 = getTimer() for i = 1, iterations do t2[i] = 123456789^0.5 -- t2[i] = number^0.5 end local end2 = getTimer()-start2 print("mathSqrt: "..end1) print("^0.5: "..end2)

If I run the math.sqrt(x) function or x^0.5 calculation with a number, 123456789 in the example, then doing the straight up calculation is much faster. However, if I create a variable called number and set it to 123456789 and I then run math.sqrt(number) and number^0.5, then the function is clearly faster.

What's going on here and is there something that I could do in order to capture the performance of doing the straight up calculation?