lua-users home
lua-l archive

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


Does anyone use these functions? It seems very inefficient to be calling
a function in order to perform a simple math function (that will never
do anything different than it does now).

On my box, I see about 10x speed difference on using these functions
versus just multiplying by the appropriate factor. Am I missing a good
use case for them? Would you consider it premature optimization to use a
multiplier rather than the functions?


local N = 1000000
local numbers = { 0, 45, 90, 135, 110.5, 180, 360,
                  math.pi, math.pi / 4, math.pi * 2 }

local t1 = os.clock()
for _,n in ipairs( numbers ) do
  for i=1,N do
    math.rad( n )
    math.deg( n )
  end
end
local t2 = os.clock()
print( "functions: " .. (t2-t1) )
--> functions: 4.374

local DEG2RAD = math.pi / 180
local RAD2DEG = 180 / math.pi

local t1 = os.clock()
for _,n in ipairs( numbers ) do
  for i=1,N do
    _ = n * DEG2RAD
    _ = n * RAD2DEG
  end
end
local t2 = os.clock()
print( "factors: " .. (t2-t1) )
--> factors: 0.438