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

Which method is more optimized?

Asked by 7 years ago

I'm trying to keep things as optimized as possible, and I was wondering if BrickColor.Black() is faster than BrickColor.new("Black"). Do any of you know?

0
Don't do either -- they return constants BlueTaslem 18071 — 7y
1
Why the hell is this downvoted. The man wants to know the answer. rexbit 707 — 7y

2 answers

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

Don't do either. Don't recompute something when you know the answer in advance.

local BLACK = BrickColor.new("Black")

Using BLACK will involve no work at all, in comparison to BrickColor.foo() which involves (1) looking up BrickColor in the global table, (2) looking up whatever method you're calling in the BrickColor table, and (3) calling that function.


Do less work. Two things which are basically the same don't make a difference. Stop obsessing over "optimizing" code in this way. The real gains are from better algorithms.

Ad
Log in to vote
2
Answered by
P100D 590 Moderation Voter
7 years ago

If you're testing for efficiency, try logging tick() before and after execution.

Here's an example of the kind of code you would write to test the BrickColor speeds:

a = tick()
c = BrickColor.Black()
a = tick() - a

b = tick()
d = BrickColor.new("Black")
b = tick() - b

print("A = " .. a)
print("B = " .. b)

This returns

A = 4.7683715820313e-006
B = 5.9604644775391e-006

So BrickColor.Black() is indeed faster than BrickColor.new("Black")

Answer this question