简体   繁体   中英

What is faster: '-isKindOfClass:' or '-isEqualToString:'?

I'm making a game in SpriteKit and want to know whether the touched node is a specific one I'm looking for. Which would be faster, giving all nodes of that custom class a NSString name and using -isEqualToString: to check it or using -isKindOfClass: to check for the specific class?

Class comparisons will almost certainly be the cheaper/more efficient of these two options. String comparison is expensive because strings need flexibility in their size (also, NSString is a cluster, the same string might be represented with an alternate data representation depending on how it was initialised) where 'class' is a known type of fixed memory footprint.

While I have never used SpriteKit, from a game development/performance standpoint string compares are very inefficient. A faster way is to instead store a hashed string and compare against that, which is just an integer compare.

I hope this helps in deciding what is best for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM