mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Improved lume.distance()'s performance on non-JIT
Replaced use of the exponent operator with multiplications, yielding a performance increase on non-JIT Lua: [5000000 calls, Lua 5.1] old func: 2.03 seconds new func: 1.17 seconds [5000000 calls, Lua 5.2] old func: 1.60 seconds new func: 0.89 seconds [2000000000 calls, LuaJIT 2.0.2] old func: 0.89 seconds new func: 0.89 seconds
This commit is contained in:
4
lume.lua
4
lume.lua
@@ -43,7 +43,9 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.distance(x1, y1, x2, y2, squared)
|
function lume.distance(x1, y1, x2, y2, squared)
|
||||||
local s = (x1 - x2) ^ 2 + (y1 - y2) ^ 2
|
local dx = x1 - x2
|
||||||
|
local dy = y1 - y2
|
||||||
|
local s = dx * dx + dy * dy
|
||||||
return squared and s or math.sqrt(s)
|
return squared and s or math.sqrt(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user