Is division slower than multiplication?

Which is faster? 10/2 or 10*.5 ?

Just wondering how different performance is between Multiplication and Division in RBLXLua

Multiplication is faster to an extent but it’s negligble such that expressiveness matters more.

6 Likes

Refer to the first and second reply to:

Answer: it is a negligible/non-measurable difference, use whatever is clearest to you, premature optimization / micro-optimization is the source of all evil.

10 Likes

Interesting…


It’s a fun topic for people with OCD.


	local Start = tick()
	
	for i = 1 , 10000 do
		local X = 10/2
	end
	
	print("Division:",Start-tick())
	
	local Start = tick()
	
	for i = 1 , 10000 do
		local X = 10*.5
	end
	
	print("Multiplication:",Start-tick())

Results;

Division: -0.0001065731048584

Multiplication: -0.00010228157043457

tick() - Start*

1 Like

You get different results every time you run it. On my device the winner varies between the two and also the absolute values vary by ~20%, meaning the difference is so negligible it is not worth accounting for

1 Like

Your tests are bad. Lua does constant folding, so:

local x = 10 / 2

is compiled to

local x = 5. Your tests are not measuring the actual speed of division and multiplication.

5 Likes

So what does a real multiplication and division test look like?

Your test inputs should not be constant, for example

local random = math.random(100,1000)
local inverse = 1/random

local startTime = tick()	
for i = 1 , 100000 do
	local X = 10/random
end

print("Division:",startTime-tick())

local startTime = tick()
for i = 1 , 100000 do
	local X = 10*inverse
end

print("Multiplication:",startTime-tick())
1 Like

Good point… rerunning the tests, these are my results.
Specs:

  • 64 bit Debian
  • i7-7800X @ 3.50GHz (4 cores, x3 threads)
  • x4 quad channel ram (configured @ 2133 MT/s)
  • eNVM SSD

Command: time lua ~/Documents/test.lua

Multiplication Test:

local function getRnd()
	return math.random(1e8, 1e9)
end

local a = getRnd()
local b = getRnd()

for i = 1, 1e8 do
	local _ = a*b
end

Division Test:

local function getRnd()
	return math.random(1e8, 1e9)
end

local a = getRnd()
local b = getRnd()

for i = 1, 1e8 do
	local _ = a/b
end

Control:

local function getRnd()
	return math.random(1e8, 1e9)
end

local a = getRnd()
local b = getRnd()

for i = 1, 1e8 do
	local _ = a
end

Results:

Multiplication:
real 0m1.284s
user 0m1.284s
sys 0m0.000s

Division:
real 0m1.022s
user 0m1.022s
sys 0m0.000s

Control:
real 0m0.887s
user 0m0.887s
sys 0m0.000s

Interesting, my results say that division is FASTER… Yes, I even double checked that I didn’t get the results crossed, and these results are significant / relatively stable.

1 Like

But still not relevant. Use whatever’s more readable.

I think the sheer difference between them is shocking. Taking out the time spent on other processes as shown by the control, we are left with 0.397 seconds for the multiplication test and 0.135 seconds for the division test. That means that division is about 3 times faster than multiplication! Say WHAT?!? I tested with smaller numbers just in case multiplication with large numbers had some mysterious overhead, but I got the same result. If anyone knows why there is such a large difference in my test or has trouble replicating it, please let us know!

I get different results in Roblox Studio:

local function getRnd()
	return math.random(1e8, 1e9)
end

local a = getRnd()
local b = getRnd()

local t = tick()
for i = 1, 1e8 do
	local _ = a*b
end
print(tick() - t)

local t = tick()
for i = 1, 1e8 do
	local _ = a/b
end
print(tick() - t)

0.9564208984375
0.96046686172485

Woah, you have a beefy machine!

Interesting, when I jump onto windows on the same machine and run this code in studio:

local function getRnd()
	return math.random(1e8, 1e9)
end

local a = getRnd()
local b = getRnd()

local t = tick()
for i = 1, 1e7 do
	local _ = a*b
end
print(tick() - t)

local t = tick()
for i = 1, 1e7 do
	local _ = a/b
end
print(tick() - t)

local t = tick()
for i = 1, 1e7 do
	local _ = a
end
print(tick() - t)

I get:

  1.2560431957245
  1.2793955802917
  1.2476074695587

Which means that in studio multiplication is about 3.8 times faster than division:

0.0084357261657999 -- multiplication - control
0.031788110733 -- division - control
3.7682720026967 -- adjusted div / adjusted mul

Interesting… why would I get such polar opposite results outside of Roblox?

Results from @Kampfkarren

1.034544467926

1.0435712337494


Results from @IdiomicLanguage

0.1039502620697

0.10268878936768

0.1026496887207

Where are these results from? I couldn’t find those figures above. Is this running our posted tests?

Yes, from both of your Code.

I ran it with Roblox Studio Command Bar

I am locking this thread. Do not ask questions like “Is X faster than Y?”, as there is no definite answer (since the difference is so small, results may differ between platform, architecture, etc), and even if test results are consistent, more often than not there is no noteworthy difference.

Only make threads concerning performance if you are actually experiencing performance issues.

5 Likes