Reginald
Reginald

Reputation:

Objective-C: which more efficient -- test IF NOT or just set a variable again?

The current value of a variable may be "X" or "Y".

A function needs to make sure it is "X".

In general -- with say C integers-- which is more efficient:

"if not X, then set to X"

"just set it to X anyway"

And does that change when the "value" is an Objective-C (immutable) object that has to get re-created?

And in both cases is this something everyone agrees on, or a debatable matter?

Upvotes: 2

Views: 394

Answers (3)

zoul
zoul

Reputation: 104065

The performance difference is usually so small that for most people it simply does not matter. I do what “reads better.” If you are in the situation where the difference might matter, profile.

Upvotes: 3

Tom Duckering
Tom Duckering

Reputation: 2737

Surely if you're dealing with an object the comparison requires invocation of a method.

Therefore you might as well just set it.

Upvotes: 0

Chris S
Chris S

Reputation: 65426

Just set it anyway would be more efficient, unless X contains some huge amount of data or you're inside a large loop.

Upvotes: 5

Related Questions