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 开发者_如何学Cto get re-created?
And in both cases is this something everyone agrees on, or a debatable matter?
Just set it anyway would be more efficient, unless X contains some huge amount of data or you're inside a large loop.
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.
Surely if you're dealing with an object the comparison requires invocation of a method.
Therefore you might as well just set it.
精彩评论