Which is BETTER for Performance?

I heard that division is more expensive than multiplication. I have a script that divides A LOT, and I wondered if there is like, I don’t know, 1000 players the game would be slower.

I am making a camera shake and FOV change script, and I want to know if x * 0.1 is faster than x / 10 when it runs this same code many times.

This is a short topic and I don’t need much discussion. It’s only for the sake of knowing which is better for performance. If you need the script just for an example, you can ask!

Thank you in advance! :slight_smile:

Let me know if my code is not correct

local x = 1
local currenttime = os.clock()
x = x*0.1
print(os.clock()-currenttime)

x = 1
currenttime = os.clock()
x = x/10
print(os.clock()-currenttime)

Outputs
13:50:14.254 4.00003045797348e-07 - Server - a:4
13:50:14.254 5.00003807246685e-07 - Server - a:9

However, it always varies

1 Like

It seems right, but I’m not a person who knows anything about computers on the binary level. I’ll take your word for it!

I wouldn’t suggest following any reply given about something so small. Performance on arithmetic operations, such as dividing or multiplying, are extremely fast.

-- Dividing
local Variable = 1
local DebugTime = os.clock()
for _ = 1, 10000 do
	Variable /= 10
end
print(os.clock() - DebugTime)
--Time taken: 0.00007559999357908964s
-- Multiplying
local Variable = 1
local DebugTime = os.clock()
for _ = 1, 10000 do
	Variable *= 0.1
end
print(os.clock() - DebugTime)
-- Time taken: 0.00005400000372901559s

The reply by @TestAccount563344 is somewhat misleading, because their output shows 4.00003… and so on, making it look bigger than it really is. Based on my performance script above, if you were to divide or multiply 1,000,000 times in one frame, it still wouldn’t cause lag, no matter the option you chose.

For reference on how little this matters, your code should only be running 60 times in one frame. The performance measured in seconds has so many decimals because of how small it is that Roblox has to use e to signal that it has more decimal places than it would like to count.

Also, if this code is running in a local script, it doesn’t matter if 10 people are playing or 10,000, because the code is only being ran on one client each.

To sum this all up, us whatever you want and what makes most sense for you to use. Optimize your code for big functions, like hitboxes, or plot building tools, etc. Optimizing something this negligible is considered micro-optimizations and it hurts your development workflow if you continue on this path.

1 Like

Yeah, I agree with this. I am not good at benchmarking anything, but I already knew that these operations do not take long at all.

Sorry for possibly spreading misinformation.

2 Likes