开发者

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

开发者 https://www.devze.com 2022-12-17 17:08 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号