Attributes are 4x slower than other reads

Just curious if anyone has done any testing to determine the efficiency of using attributes and it’s associated functions. Is it equivalent to just reading properties or is it less efficient? I’d like to start using more GetAttribute()'s on a frame by frame basis but I’m concerned it might have some limitations on performance.

EDIT found out attributes are kinda terrible.

Actually nevermind, just tested it myself and GetAttribute is about 4x slower than reading properties directly. Here’s my test code if you’re interested:

local st = os.clock()
for i = 1,50000 do
local se = workspace.StreamingEnabled
end
local en = os.clock()
print((en-st)*1000)

local st = os.clock()
for i = 1,50000 do
local se = workspace:GetAttribute('StreamingEnabled')
end
local en = os.clock()
print((en-st)*1000)

Output was:
3.3882999850903
12.338999978965

While the time difference here isn’t bad by any metric, I think Roblox really needs to speed up the Attributes system, otherwise it’s still objectively worse than just using the old style of putting value objects in things.

Function calls are inherently going to take longer than a single indexing. Your average time for GetAttribute is .000247 seconds, which is utterly insignificant. The only time this will make an impact is in a loop with no yield as you have created, which is largely unnecessary for most gameplay. If it is somehow necessary, then the big holdup in code execution will be whatever else they are doing in the loop, such as setting BrickColor. If it is still an issue, then this person either needs to find a different system (such as caching) or he can feel free to use the old ValueBase system.

Oh, and one more note. Based on your GetAttribute numbers, it can be run 67 times per frame by itself. This means that you have some leeway to work with. You could, for example, call GetAttribute once per player for every player in a twenty player game every frame and still have plenty of time left in the frame to do stuff with the data. However don’t think that you need to be running systems like this every single frame in most cases.

2 Likes

The time difference between accessing value instances & attributes is negligible.

I feel like you didn’t really catch the point I was trying to make.

Attributes were added supposedly as a better way of handling custom variables outside of scripts. It’s more of a hassle to reference attributes, set attributes, and they even perform worse. I expected it to be marginally worse, but 4x slower? Come on, that’s ridiculous. So the attributes system is a complete failure at what it was designed for.

Also your math appears to be off, I multiplied the time difference by 1000 so it would output coherently, so 50k calls of GetAttribute only took 12ms or 0.012 seconds. You could call GetAttribute 68,750 times and still be at 60FPS. Not that one ever would.

It’s a function call, programming isn’t magic. It is better, it’s just slower than traditional indexing. This is always the case. It’s why people should use workspace["Two words"] instead of workspace:FindFirstChild("Two words")

You’re right, I missed that. That makes it even less of an issue.

Is it really more of a hassle to reference and set them? They are better organized and take up less memory, and the only real cost is a little bit of typing speed. It is definitely a up to you as a preference, but the speed is neither unexpected nor an issue.

2 Likes

I mean that only works if the object actually exists, I use FindFirstChild often when I have streaming enabled.

3 Likes