Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Quick Question] Which one of these value comparisons is the fastest and slowest?

As you learn knowing how to code/script or whatever you call it. There are comparisons, and they are a compiler to check the condition if it is it or it isn't it. So, I wonder, which one in these comparison values is the fastest or slowest for performance in Lua?

--//STRINGS
local str1 = 'Fun'
local str2 = 'Not Fun'

if str1 == str2 then print("They are the same yo!")
else print("They ain't the same yo!") end

--//INTEGERS
local num1 = 123
local num2 = 321

if num1 == num2 then print("They are the same yo!")
else print("They ain't the same yo!") end

--//REAL NUMBERS (Eg. 156.723)
local real1 = 123.321
local real2 = 321.123

if real1 == real2 then print("They are the same yo!")
else print("They ain't the same yo!") end

--//OBJECT VALUES
local part1 = workspace.Part1
local part2 = workspace.Part2

if part1 == part2 then print("They are the same yo!")
else print("They ain't the same yo!") end

Note: If you're about to comment or answer "It doesn't work that way! Comparisons doesn't have any speed comparisons", then it's not right, values does have different speeds in comparisons, you may search it up on the Internet for proof and use the C++ Performance Debug or the JavaScript Code Debugger

3 answers

✓ Accepted answer

I would be shocked if there was any measurable difference, because the way Lua is designed allows it to do the check for each kind of object in basically the same way.

Measuring empirically, I got that == on each type takes between 0.1 and 0.2 microseconds on my computer (and all the types takes the same time to compare -- each varying in the same window).


Lua 5.1 (the version that Roblox uses) does not distinguish between integers and fractional numbers. They are all stored as 64 bit floating point numbers.

Here is the implementation of equality in Lua:

https://www.lua.org/source/5.1/lobject.c.html#luaO_rawequalObj

First, it checks if the types differ. The cost of doing this would be the same in all cases, because ttype just reads the same single word from the values.

Next, it chooses the right type. Doing this should be basically instant through cleverness in compiling C (and approximately uniform for all the types).

For numbers, the values are compared with C's == on floats. This will be essentially a single machine instruction, and one of the fastest.

For strings and tables, their addresses are compared (with C's pointer ==). This will be a single machine instruction, and basically the same speed as floating point. This is different from C++!

String comparison

In some languages, strings are compared by marching through the characters in them to see if they are different.

Lua does not do this, because it does string interning. This means that two strings that have the same content are stored in the same place. This means that == is as simple as determining if the place is the same -- which is just comparing two addresses (numbers).

In order to do this, every time that you create a string, Lua must walk over the string to figure out if you already have it! This unfortunately means that a .. b cannot be done in better that linear time and that a:sub(1, -2) requires copying the entire substring. Most of the time, we expect these operations to take that much time anyway, so we don't notice that Lua is doing this thing so that == becomes fast!

Optimization

Even so, you probably don't need to care about this. Almost surely, less than a fraction of a percent of your program's runtime is spent comparing objects for equality. So even if you make these comparisons 1000x faster, your program's overall speed can't improve by more than a fraction of a percent!

See here on the devforum for a longer post I've made on this.

Since I’m on mobile and I don’t really focus on script performance, I don’t have an answer for this. I did find a link though that’ll tell how much CPU a script is using so you can see which is better or worse.

Here’s the link :

http://wiki.roblox.com/index.php?title=Script_Performance

2 comments

0

I'm sorry but before posting this I've already have a long research on the wiki. As it works that the rate slightly changes in real numbers as 1/s and 1.00001/s so it's really hard to recognize the differences between the two values

0

Ah, I see. I can’t really think of anything that can help you now. Sorry. :I

Get the elasped time.

local function Compare(object1,object2,dataType)
	local startTick = tick()

	if object1 == object2 then print("They are the same yo!")
	else print("They ain't the same yo!") end

	print(dataType..": "..(tick() - startTick)*1000, 'ms')
end

--//STRINGS
local str1 = 'Fun'
local str2 = 'Not Fun'
Compare(str1,str2,"Strings")

--//INTEGERS
local num1 = 123
local num2 = 321
Compare(num1,num2,"Integers")

--//REAL NUMBERS (Eg. 156.723)
local real1 = 123.321
local real2 = 321.123
Compare(real1,real2,"Real Numbers")

--//OBJECT VALUES
local part1 = workspace.Part1
local part2 = workspace.Part2
Compare(part1,part2,"Objects")

These are my times but yours might be slightly different. Integers seem to be the fastest with strings being the slowest.

They ain't the same yo!
Strings: 2.5146007537842 ms
They ain't the same yo!
Integers: 0.7469654083252 ms
They ain't the same yo!
Real Numbers: 0.84376335144043 ms
They ain't the same yo!
Objects: 0.93483924865723 ms

2 comments

0

This is totally within the noise. You are measuring the time to print(), not the time to do ==.

0

Believing that == takes a full 2 milliseconds is absurd. The correct figure is less than a fifth of a microsecond, and is the same across all types.

This discussion is preserved as it was. New answers, comments, and votes are closed.