I was looking through ROBLOXs slingshot script and saw this:
(VELOCITY*VELOCITY*VELOCITY*VELOCITY)
Is it more optimized to multiply the velocity 4 times like that? Because from what I know you could also do this:
math.pow(VELOCITY,4)
Is there any difference between the two? Is it more optimized? Or math
wasn't available at the time they made this model?
math.pow
is almost certainly faster... in the long run.
Benchmarking multiplications Two mults: math.pow: 0.34422492980957 mult: 0.45623826980591 difference: 0.11201333999634 ---------------- Three mults: math.pow: 0.28800177574158 mult: 0.41197323799133 difference: 0.12397146224976 ---------------- Four mults: math.pow: 0.29200673103333 mult: 0.41459703445435 difference: 0.12259030342102 ---------------- Five mults: math.pow: 0.28451442718506 mult: 0.41259717941284 difference: 0.12808275222778 ---------------- Six mults: math.pow: 0.28455901145935 mult: 0.43236207962036 difference: 0.14780306816101 ----------------
This was generated using the following code.
Depending on how infrequently the calculation is made, the direct multiplication may actually be faster, but it's likely just a holdover from C/C++ where the direct mult is almost certainly faster than the function. Lua's just weird.
I only did 1 million iterations, and didn't do any statistical analysis so the results aren't perfect or exact. I also used very large numbers, so it's entirely possible that for very small numbers direct mult is faster.
Ultimately, use whichever you prefer. Half a second to do 1 million increasingly large multiplications is faster than anyone should ever need to care about at the level ROBLOX gives up for development.
print("Benchmarking multiplications") print("") print("Two mults:") st = tick() for i = 1, 1000000 do a = math.pow(i, 2) end et = tick() - st print("math.pow: " .. et) st2 = tick() for i = 1, 1000000 do a = i*i end et2 = tick() - st print("mult: " .. et2) print("difference: " .. math.abs(et - et2)) print("----------------") print("Three mults:") st = tick() for i = 1, 1000000 do a = math.pow(i, 3) end et = tick() - st print("math.pow: " .. et) st2 = tick() for i = 1, 1000000 do a = i*i*i end et2 = tick() - st print("mult: " .. et2) print("difference: " .. math.abs(et - et2)) print("----------------") print("Four mults:") st = tick() for i = 1, 1000000 do a = math.pow(i, 4) end et = tick() - st print("math.pow: " .. et) st2 = tick() for i = 1, 1000000 do a = i*i*i*i end et2 = tick() - st print("mult: " .. et2) print("difference: " .. math.abs(et - et2)) print("----------------") print("Five mults:") st = tick() for i = 1, 1000000 do a = math.pow(i, 5) end et = tick() - st print("math.pow: " .. et) st2 = tick() for i = 1, 1000000 do a = i*i*i*i*i end et2 = tick() - st print("mult: " .. et2) print("difference: " .. math.abs(et - et2)) print("----------------") print("Six mults:") st = tick() for i = 1, 1000000 do a = math.pow(i, 6) end et = tick() - st print("math.pow: " .. et) st2 = tick() for i = 1, 1000000 do a = i*i*i*i*i*i end et2 = tick() - st print("mult: " .. et2) print("difference: " .. math.abs(et - et2)) print("----------------")