From a08436445d7931a61e0a3ae801f2151eac87dc55 Mon Sep 17 00:00:00 2001 From: rxi Date: Wed, 5 Mar 2014 12:18:49 +0000 Subject: [PATCH] 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 --- lume.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lume.lua b/lume.lua index 8140a72..3f8a501 100644 --- a/lume.lua +++ b/lume.lua @@ -43,7 +43,9 @@ end 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) end