Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Multiplying the value 4 times VS using math.pow(x,4)?

Asked by 7 years ago

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?

0
I'll do some benchmarks and get back to you. In my opinion, the function is faster here, but I'll check. adark 5487 — 7y
0
Probably use ^4 instead of either BlueTaslem 18071 — 7y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
7 years ago

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("----------------")
Ad

Answer this question