# Optimization: Calculate Roots Speed (plus handy root function!)

**URL:** <https://forums.solar2d.com/t/optimization-calculate-roots-speed-plus-handy-root-function/319505>\
**Category:** Lua Language\
**Created:** [November 4, 2013, 8:15pm UTC](https://forums.solar2d.com/t/optimization-calculate-roots-speed-plus-handy-root-function/319505 "2013-11-04T20:15:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 4, 2013, 8:15pm UTC](https://forums.solar2d.com/t/optimization-calculate-roots-speed-plus-handy-root-function/319505/1 "2013-11-04T20:15:39Z")

</div>

Just a quick heads-up for anyone who reads this - to calculate a square root (or any other root, for that matter),&nbsp; **use Lua math equations, not math.sqrt**.

According to the test I made, using the equivalent x ^ 0.5 is approximately four times faster than localized math.sqrt. I knocked up a simple test for it, and here’s my result:

Using localized math.sqrt:

[lua]

Test Finished:&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;50000000 calculations

&nbsp;&nbsp;&nbsp;&nbsp;3.718415 secs taken

&nbsp;&nbsp;&nbsp;&nbsp;74.3672 ms on average

Test Finished:&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;50000000 calculations

&nbsp;&nbsp;&nbsp;&nbsp;3.713201 secs taken

&nbsp;&nbsp;&nbsp;&nbsp;74.26284 ms on average

Test Finished:&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;50000000 calculations

&nbsp;&nbsp;&nbsp;&nbsp;3.723258 secs taken

&nbsp;&nbsp;&nbsp;&nbsp;74.46406 ms on average

[/lua]

Using 100 ^ 0.5:

[lua]

Test Finished:&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;50000000 calculations

&nbsp;&nbsp;&nbsp;&nbsp;0.971279 secs taken

&nbsp;&nbsp;&nbsp;&nbsp;19.42424 ms on average

Test Finished:&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;50000000 calculations

&nbsp;&nbsp;&nbsp;&nbsp;0.973754 secs taken

&nbsp;&nbsp;&nbsp;&nbsp;19.47412 ms on average

Test Finished:&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;50000000 calculations

&nbsp;&nbsp;&nbsp;&nbsp;0.940703 secs taken

&nbsp;&nbsp;&nbsp;&nbsp;18.81362 ms on average

[/lua]

Here’s my test (comment/uncomment the lines to use different methods):

[lua]

local print = print

local math\_sqrt = math.sqrt

local system\_getTimer = system.getTimer

local function test(iterations)

&nbsp;&nbsp;&nbsp;&nbsp;local time = 0

&nbsp;&nbsp;&nbsp;&nbsp;local totalTime = 0

&nbsp;&nbsp;&nbsp;&nbsp;local testStart = system\_getTimer()

&nbsp;&nbsp;&nbsp;&nbsp;for i = 1, iterations do

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local startTime = system\_getTimer()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i = 1, 1000000 do

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--local t = 100 ^ 0.5

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local t = math\_sqrt(100)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local testTime = system\_getTimer() - startTime

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time = time + testTime

&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;time = time / iterations – Average the result

&nbsp;&nbsp;&nbsp;&nbsp;totalTime = system\_getTimer() - testStart

&nbsp;&nbsp;&nbsp;&nbsp;local result = “\n\nTest Finished: \n\t” … iterations \* 1000000 … " calculations\n\t" … totalTime / 1000 … " secs taken\n\t" … time … " ms on average"

&nbsp;&nbsp;&nbsp;&nbsp;print(result)

end

for i = 1, 3 do

&nbsp;&nbsp;&nbsp;&nbsp;test(50)

end

[/lua]

There’s the root speed part of the title. Now, for the “handy root function” part - you can calculate the nth root of any number with this quick function:

[lua]

local function nroot(n, r) return n ^ (1 / r) end

local result1 = nroot(50, 3) – The cube root of 50

local result2 = nroot(128.5, 9) – The ninth root of 128.5

[/lua]

Anyhow… Just testing out some root calculations 🙂

- Caleb
